【问题标题】:How to take data from one dataframe and copy it into existing columns in another dataframe based on the shared ID of a third column如何根据第三列的共享 ID 从一个数据框中获取数据并将其复制到另一个数据框中的现有列中
【发布时间】:2020-11-25 01:30:52
【问题描述】:

所以我有两个不同的数据框:一个是我一直在处理的数据框 (df1),另一个是我需要放入第一个数据框的所有新数据 (df2)。 Df1 有几列零,等待添加数据。 Df2 有我需要的数据,以及除该数据之外我不关心的更多行和列。这是我正在使用的数据类型的一小部分。

这是我第一次发布我的数据,所以我希望我做得对。如果您需要其他格式,请告诉我。

df1:

structure(list(season = c(" FA15", " FA15", " FA15", " FA15", 
" FA15", " FA15", " FA15", " FA15", " FA15", " FA15"), year = c("2015", 
"2015", "2015", "2015", "2015", "2015", "2015", "2015", "2015", 
"2015"), territory.name = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), plot = c("0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0"), color.band = c("APGBY", 
"APGGU", "APGPW", "APGPW", "APGR", "APGUO", "APGUO", "APGUO", 
"APGUO", "APGYR")), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

df2:

structure(list(bandnum = c(157328052, 160379101, 157328094, 151313455, 
170364680, 160379104, 151373458, 157328066, 160379103, 160379105
), project = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L), .Label = c("*ISSJ", "ISSJ"), class = "factor"), color.band = c("PAWR", 
"WYWAR", "APGP", "APGO", "ABYG", "URYAR", "APBW", "WABG", "OBWAR", 
"GBGAR"), sex = structure(c(3L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 
2L), .Label = c("?", "F", "M"), class = "factor"), age = structure(c(2L, 
1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L), .Label = c("AHY", "ASY", 
"HY", "N", "SY"), class = "factor")), row.names = c(NA, 10L), class = "data.frame")

几天来我一直在研究这个问题,尝试了不同的方法并阅读了很多关于堆栈溢出的答案,但我未能就如何从一个数据帧中获取数据并复制给出一个明确的答案根据第三列的共享 ID 将其放入另一个数据框中的现有列中。

差不多,我希望 r 看到两个数据帧在 color.band 列中都有一个带 ABCDEF 的列表,然后从 df2$bandnum 中获取与 ABCDEF 相同行的值并将其复制到 df1$那里的 ABCDEF 行中的bandnum。

我不想将 df2 中但不是 df1 的行复制到 df1 中。我想在 bandnum 列中将 df1 但不存在 df2 的条目标记为 N/A。

色带和色带编号的列名和数据格式已在两个数据框之间标准化,因此所有内容都应对齐。 到目前为止我的代码是这样的:

> practicedf <- left_join(x=df1, y=df2, by = "color.band", all.x = TRUE) 
%>% mutate(y = ifelse(is.na(df1$color.band), df1$bandnum, df1$color.band)) %>% select(df2$bandnum)

left_join 似乎是正确的,因为它将所有行保留在左侧 (df1) 数据框中,并且仅匹配右侧 (df2) 数据框中的行。 我得到了这个错误:

Error in `[[<-.data.frame`(`*tmp*`, col, value = c("APGBY", "APGGU", "APGPW", : 
replacement has 1261 rows, data has 2559

color.band 是字符向量,而 bandnum 是数字,有问题吗?这可能是什么问题?


编辑:我在两个数据帧中都包含 bandnum 列时出错,因此我将 df2$bandnum 更改为 bandnum.y。我的代码现在是

df1_test <- left_join(x=df1, y=df2, by = "color.band") %>% mutate(y =
 ifelse(is.na(color.band), bandnum, color.band)) %>% select(bandnum.y) 

但是当我查看(df1_test) 时,它只显示了bandnum.y 列,它与我原来的df1 的条目数不同

这是 df1_test 的一个子集(不是全部,因为它有 2600 个条目)

有什么方法可以让它也显示我的其余数据?

structure(list(bandnum.y = c("171324972", "171324972", "171324972", 
"178324697", "178324697", "178324697", "178324697", "178324697", 
"178324697", "178324697", "170364505", "170364505", "170364505", 
"170364505", "170364505", "170364505", NA, "178324692", "178324692", 
"178324692")), row.names = c(NA, -20L), class = c("tbl_df", "tbl", 
"data.frame"))

【问题讨论】:

  • 在您的ifelse 中,您从df1 获取值。相反,它会没有df1$,因为这些列已经在连接的数据集中,并且因为您使用了all.x = TRUE,它可以比原始数据集“df1”拥有更多的行数

标签: r dataframe dplyr


【解决方案1】:

我们不能在连接后使用原始数据集“df1”列,因为它是left_join。在tidyverse 中,我们指定了不带引号的列名。 left_join 中没有 all.x 参数。应该来自merge

library(dplyr)
left_join(x=df1, y=df2, by = "color.band") %>% 
     mutate(y = ifelse(is.na(color.band), bandnum, color.band)) 

【讨论】:

  • 我从那里收到一条错误消息,提示“错误:无法对不存在的列进行子集化。x 列 bandnum 不存在。”即使 bandnum 作为列存在于 df1 和 df2 中。为什么会这样?
  • @SarahHays 在您的示例中,只有“df2”显示“bandnum”,“df1”中没有“bandnum”。如果两个数据集都有“bandnum”,那么您可能需要从其中一个中删除该列之一。否则会变成bandnum.x和bandnum.y
  • 谢谢!还有一个问题——当我运行 view(df2) 时,它运行并吐出了一列充满波段号的列,但我的其他列都不存在。我该如何保留它们?
  • @SarahHays 如果这是您的评论的意思,即out &lt;- left_join(df1, df2, ...,您可能需要将输出分配给一个新对象
  • @SarahHays 好的,您使用select 作为最后一步。然后,它将仅选择该列。删除它,然后您将拥有所有列。我在想你只需要'bandnum'列。可能是 select 被用作某种输出检查,后来忘记删除那些
【解决方案2】:

left_join 没有 all.x = TRUE 是基础 R 的一部分 merge。

您可以在基础 R 中执行以下操作:

df1_test <- transform(merge(df1, df2, by = "color.band", all.x = TRUE), 
                      y = ifelse(is.na(color.band), bandnum, color.band))

【讨论】:

    【解决方案3】:

    如果我没有误会,您想使用新 df (df2) 中的信息更新旧 df (df1)。 在data.table,你可以试试这个:

    libraty(data.table)
    setDT(df1)
    setDT(df2)
    update.vars = intersect(names(df1), names(df2))  # update only common variables
    
    df1[df2, c(update.vars) := df2[,update.vars, with=FALSE], on= 'color.band'] 
    

    通常这应该有效。但是在给定的数据中,“合并”ID(color.band 列)不是唯一的,这可能会影响结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-26
      • 2018-01-03
      • 2018-08-03
      • 1970-01-01
      • 2019-09-28
      • 2019-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多