r - How to copy values from data.frames with different length? -
i'm trying extract specific values 1 data.frame another. frames have different length similar names (i.e. 'a'::'z'
, 'a'::'w'
)
names <- letters[1:26] df1 <- data.frame("name" = names[1:20],"value" = rnorm(20, mean = 4, sd = 1)) df2 <- data.frame("name" = names[1:26],"value" = rnorm(26, mean = 4, sd = 1)) df2$value2 <- df1[df2$name %in% df1$name,]$value
the last line above not work produces following error:
error in `$<-.data.frame`(`*tmp*`, "value2", value = c(4.21005563122984, : replacement has 20 rows, data has 24
any suggestions how produce this:
name value value2 1 4.210056 5.918197 2 b 3.203976 4.485109 3 c 4.290336 4.210056 ...... 25 y 5.918197 na 26 z 3.861640 na
df2$value2[df2$name %in% df1$name] <- df1$value
Comments
Post a Comment