replace - R: How to do fastest replacement in R? -


i have input dataframe (the real 1 large, want faster):

df1 <- data.frame(a=c(1:5), b=c(5:9), c=c(9:13))    b  c 1 1 5  9 2 2 6 10 3 3 7 11 4 4 8 12 5 5 9 13 

i have dataframe replacement code (the entries here maybe more df1):

df2 <- data.frame(x=c(1:15), y=c(101:115))      x   y 1   1 101 2   2 102 3   3 103 4   4 104 5   5 105 6   6 106 7   7 107 8   8 108 9   9 109 10 10 110 11 11 111 12 12 112 13 13 113 14 14 114 15 15 115 

by matching df2$x value in df1$a , df1$b, want new_df1 replace df1$a , df1$b corresponding values in df2$y, i.e. resulting new_df1

     b    c 1 101  105  9 2 102  106 10 3 103  107 11 4 104  108 12 5 105  109 13 

could mind give me guidance how faster in r, dataframe large? many thanks.

it's supereasy! need proper offsets in array.

so instance, y column of df2 corresponding values in column of df1 you'll write df2$y[df1$a]

hence, code be:

df_new <- data.frame("a" = df2$y[df1$a], "b" = df2$y[df1$b], "c" = df1$c) 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -