[R] Clever way to match two lists

Jim Holtman jholtman at gmail.com
Fri Mar 22 02:07:26 CET 2013


indx <- match(main.df$id, key$id, nomatch = 0)
main.df$value[indx != 0] <- key$value[indx]

Sent from my iPad

On Mar 21, 2013, at 20:57, Noah Silverman <noahsilverman at ucla.edu> wrote:

> Hello,
> 
> I have a large data.frame of 80,000 rows where each row is a record.  Each record is indexed by a unique ID in the first column.
> 
> I need to update values for a column for *some* of the records.  I was given a data.frame with about 10,000 rows and two columns.  The first is the record ID, the second is the new values.
> 
> The slow way to do this is to loop through the new data (10,000 rows) and update the value in the corresponding master data.frame.  However this will be painfully slow.  
> 
> Does anyone have a clever way to shortcut the process.  In English, what I want to do is, "For each row in the update list, lookup the corresponding ID in the master data frame and update the value."
> 
> 
> Ideas?
> 
> 
> --
> Noah Silverman, M.S.
> UCLA Department of Statistics
> 8117 Math Sciences Building
> Los Angeles, CA 90095
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list