有没有一种方法可以用R中的另一个数据框替换行中的数据?

我试图弄清楚如何通过匹配其中一个列的值来用另一个替换一个数据框中的行。两个数据框具有相同的列名。

例如:

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替换值。

有谁知道如何做到这一点?

评论
(OoO)
(OoO)

A join option with data.table where we join on the 'x' column, assign the values of 'y' in second dataset (i.y) to the first one with :=

library(data.table)
setDT(df1)[df2, y := i.y, on = .(x)]
点赞
评论
pculpa
pculpa

我认为您这里需要的是“合并”或“加入”操作。

(I add stringsAsFactors=FALSE to the frames so that the merging and later work is without any issue, as factors can be disruptive sometimes.)

基数R:

df1 <- data.frame(x = c(1,2,3,4), y = c("a", "b", "c", "d"), stringsAsFactors = FALSE)
# df2 <- data.frame(x = c(1,2), y = c("f", "g"), stringsAsFactors = FALSE)
merge(df1, df2, by = "x", all = TRUE)
#   x y.x  y.y
# 1 1   a    f
# 2 2   b    g
# 3 3   c <NA>
# 4 4   d <NA>
transform(merge(df1, df2, by = "x", all = TRUE), y = ifelse(is.na(y.y), y.x, y.y))
#   x y.x  y.y y
# 1 1   a    f f
# 2 2   b    g g
# 3 3   c <NA> c
# 4 4   d <NA> d
transform(merge(df1, df2, by = "x", all = TRUE), y = ifelse(is.na(y.y), y.x, y.y), y.x = NULL, y.y = NULL)
#   x y
# 1 1 f
# 2 2 g
# 3 3 c
# 4 4 d

Dplyr:

library(dplyr)
full_join(df1, df2, by = "x") %>%
  mutate(y = coalesce(y.y, y.x)) %>%
  select(-y.x, -y.y)
#   x y
# 1 1 f
# 2 2 g
# 3 3 c
# 4 4 d
点赞
评论