【问题标题】:Populate New Variable In Data Frame Using Relationship Between Two Variables In Another Data Frame使用另一个数据框中的两个变量之间的关系填充数据框中的新变量
【发布时间】:2019-07-21 19:45:03
【问题描述】:

我有两个具有不同 #s 观察值的数据框(一个很长,有 2220 个 obs,另一个很宽,有 37 个 obs)。数据帧共享变量“SID”,尽管在长数据帧中每个 SID 值有 60 行,而在宽数据帧中只有一个。宽数据帧有一个附加变量“Experimenter”,每个 SID 都有一个对应的 Experimenter 编号。我想在长数据框中创建一个“Experimenter”列,虽然每个 SID 有 60 个实例,但我希望每次 SID 值出现时添加并重复相应的 Experimenter 值(所以 60 次)。

每个主题的嵌套 if-else 命令似乎很乏味,所以我希望有替代方法

我已经添加了每个数据帧的 dput 输出,不幸的是,我不确定如何嵌入它们。现在在长数据框中,“SID”被命名为“主题”,但它们是同一个变量。

宽:

structure(list(SID = 7301:7302, Experimenter = c(2L, 1L)), .Names = c("SID", 
"Experimenter"), class = "data.frame", row.names = c(NA, -2L))

长:

structure(list(Subject = c(7301L, 7301L, 7301L), Session = c(1L, 
1L, 1L), Stimtype = structure(c(1L, 1L, 1L), .Label = "Control", class = 
"factor"), 
Valence = structure(c(1L, 1L, 1L), .Label = "Neutral", class = "factor"), 
Block = c(1L, 1L, 1L), Image = c(12L, 17L, 22L), Group = structure(c(1L, 
3L, 2L), .Label = c("Neutral_1660", "Neutral_5300", "Neutral_7233"
), class = "factor"), Response = c(1L, 1L, 1L), Stimulus = c(1660L, 
7233L, 5300L)), .Names = c("Subject", "Session", "Stimtype", 
"Valence", "Block", "Image", "Group", "Response", "Stimulus"), class = 
"data.frame", row.names = c(NA, 
-3L))

如果我们正在查看这些图像,我要做的就是在长数据框中插入“Experimenter”变量,只要“Subject”为“7301”(如在广泛的数据)等所有科目。

提前谢谢你。

【问题讨论】:

  • 请以可复制、可复制和粘贴的格式包含示例数据和预期输出。截图从来都不是一个好主意。有关详细信息,请查看如何提供minimal reproducible example/attempt
  • @MauritsEvers 谢谢,正在努力创建这些并更新帖子。

标签: r dataframe dataset


【解决方案1】:

除非我误解了这似乎是merge/left_join的简单案例

在基础 R

merge(df2, df1, by.x = "Subject", by.y = "SID")
#  Subject Session Stimtype Valence Block Image        Group Response Stimulus
#1    7301       1  Control Neutral     1    12 Neutral_1660        1     1660
#2    7301       1  Control Neutral     1    17 Neutral_7233        1     7233
#3    7301       1  Control Neutral     1    22 Neutral_5300        1     5300
#  Experimenter
#1            2
#2            2
#3            2

或者使用dplyr

library(dplyr)
left_join(df2, df1, by = c("Subject" = "SID"))

给出相同的结果


样本数据

df1 <- structure(list(SID = 7301:7302, Experimenter = c(2L, 1L)), .Names = c("SID",
"Experimenter"), class = "data.frame", row.names = c(NA, -2L))

df2 <- structure(list(Subject = c(7301L, 7301L, 7301L), Session = c(1L,
1L, 1L), Stimtype = structure(c(1L, 1L, 1L), .Label = "Control", class =
"factor"),
Valence = structure(c(1L, 1L, 1L), .Label = "Neutral", class = "factor"),
Block = c(1L, 1L, 1L), Image = c(12L, 17L, 22L), Group = structure(c(1L,
3L, 2L), .Label = c("Neutral_1660", "Neutral_5300", "Neutral_7233"
), class = "factor"), Response = c(1L, 1L, 1L), Stimulus = c(1660L,
7233L, 5300L)), .Names = c("Subject", "Session", "Stimtype",
"Valence", "Block", "Image", "Group", "Response", "Stimulus"), class =
"data.frame", row.names = c(NA,
-3L))

【讨论】:

  • 谢谢毛里蒂斯。它适用于我创建的示例数据帧,但对于完整的数据帧,我得到以下错误: left_join(ExperimenterSID, eprime1, by = c("Subject" = "SID")) 错误:by can' t 包含 LHS 调用 rlang::last_error() 中缺少的连接列 Subject 以查看回溯 OR > Joined1
  • @LucyToru 这显然不容易调试,因为我们无法访问您的实际数据。这就是为什么样本数据应该代表您的实际数据。如果您的数据看起来不同和/或具有不同的列名,则需要相应地进行更改。在left_join(ExperimenterSID, eprime1, by = c("Subject" = "SID")) 中,您通过匹配条目ExperimenterSID$Subjecteprime1$SID 来连接数据。您需要确保这些列存在。
  • 数据代表实际数据。我只是切换了数据框,完全是我的错误。感谢您对我的包容。
猜你喜欢
  • 1970-01-01
  • 2019-05-22
  • 2014-08-29
  • 1970-01-01
  • 2019-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-05
相关资源
最近更新 更多