我试图弄清楚如何通过匹配其中一个列的值来用另一个替换一个数据框中的行。两个数据框具有相同的列名。
例如:
df1 <- data.frame(x = c(1,2,3,4), y = c("a", "b", "c", "d"))
df2 <- data.frame(x = c(1,2), y = c("f", "g"))
有没有办法用共享相同x变量的df2中的同一行替换df1中的行?看起来像这样。
data.frame(x = c(1,2,3,4), y = c("f","g","c","d")
我已经为此工作了一段时间,这是我最近得到的-
df1[which(df1$x %in% df2$x),]$y <- df2[which(df1$x %in% df2$x),]$y
但这只是用NA替换值。
有谁知道如何做到这一点?
A join option with
data.table
where we joinon
the 'x' column, assign the values of 'y' in second dataset (i.y
) to the first one with:=
我认为您这里需要的是“合并”或“加入”操作。
(I add
stringsAsFactors=FALSE
to the frames so that the merging and later work is without any issue, asfactor
s can be disruptive sometimes.)基数R:
Dplyr: