dataframe - Changing values between two data.frames in R -


i have following data.frames(sample):

>df1   number             action 1      1               2      2               3      3           theother 4      4             >df2       id            value 1      1                3 2      2                4 3      3                2 4      4                1 4      5                4 4      6                2 4      7                3 .      .                . .      .                . 

i df2 become following:

>df2       id            value 1      1         theother 2      2          3      3             4      4             4      5          4      6             4      7         theother .      .                . .      .                . 

it can done 'mannualy' using following each value:

df2[df2==1] <- 'this' df2[df2==2] <- 'that'        .        . 

and on, there way not mannualy?

try

df2$value <- setnames(df1$action, df1$number)[as.character(df2$value)] df2 #   id    value #1  1 theother #2  2  #3  3     #4  4     #5  5  #6  6     #7  7 theother 

or use match

df2$value <- df1$action[match(df2$value, df1$number)] 

data

df1 <- structure(list(number = 1:4, action = c("this", "that",  "theother",  "another")), .names = c("number", "action"), class = "data.frame",  row.names = c("1", "2", "3", "4"))  df2 <- structure(list(id = 1:7, value = c(3l, 4l, 2l, 1l, 4l, 2l, 3l  )), .names = c("id", "value"), class = "data.frame", row.names = c("1",   "2", "3", "4", "5", "6", "7")) 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -