Saturday 15 March 2014

To Replace/add values to column in dataframe x looking at values in data frame y in R -


Change the values ​​in columns in the datafram x / Given values ​​in the data frame y in the x

Temporary file or X is a very large data frame

  1 idname 3 unit aa jhn cc na dd m 234 ff nga gg sind ii nan .... pp .....   

The map file or the Y is a small data frame

  the name ID contact address John jhn j123 j Mary mry m234 m   

My Condition

 for  (i in 1: length (x $ 1)) {if (x $ 2 == wa $ Alt_name1 || X $ 2 == Y $ alt_name2 || X $ 2 == Y $ Alt_name3] x $ name [i] = y $ name [i]}   

this is , If any name of the name in the Y column matches the value Y $ name X matches $ 2 the related $ Y $ should be added to the exact line named name X $

whether this operation Is there any effective way to accomplish this? There were a few million rows in the x and y four lines say.

Any help is greatly appreciated.

Now what do I have for

  (in 1: length (tempFile $ unit)) {for in j: length (mapFile $ name)) {If (tempFile $ idname [i] == mapfile $ id [j]) elseif (tempFile $ idname [i] == mapFile $ contact [j]) otherwise (tempFile $ idname [i] == mapFile $ address [j ]) TempFile $ unit [i] & lt; - mapFile $ name [j]}}    

  big.df & lt ; - read.table (text = "1 2 3 name aa jhn cc NA dd m234 ff NA gg cind ii NA", header = TRUE, check.names = FALSE, as.is = TRUE) small.df & lt; - read.table (text = "name alt_name1 alt_id alt_name3 John jhn j123 j Mary Mry M234 M", header = TRUE, check.names = FALSE, as.is = TRUE) alt.names & lt; - big.df [, 1: 3] alt.key & lt; - small.df [, 2] ifelse (alt.names [alt, 1]% alt.key | alt.key | alt.names | alt.names in [, 2]% alt.key | alt.names [, 3 ]% Alt.key, alt.key, NA) / code>  

something like that should be done. Obviously you want to dry it a bit, but ifelse is vectorized and you can just pass the result to name at big.df Code>. You can use it simply match or % in% without the ifelse <{code} (which only matches as a binary operator) And it should be much faster than the loop.

No comments:

Post a Comment