r - 使用来自其他列的某些行的值创建新列

标签 r dataframe

我有一个如下所示的数据框: (示例已编辑)

df <- data.frame(Subject = c(rep("A", 9), rep("B", 8)),
Trial = c(1,1,2,3,4,4,5,6,6,1,2,2,3,4,5,5,6),
Feature_1 = c(rep(123, 2), 234, 345, rep(456, 2), 567, rep(678, 2), 831, rep(444, 2), 461, 921, rep(436, 2), 111),
Feature_2 = c(rep(321, 2), 543, 654, rep(765, 2), 876, rep(987, 2), 912, rep(302, 2), 900, 555, rep(382, 2), 197),
Feature_3 = c(rep(190, 2), 459, 392, rep(398, 2), 492, rep(587, 2), 761, rep(901, 2), 783, 312, rep(880, 2), 229),
Feature_correct = NA)

df
   Subject Trial Feature_1 Feature_2 Feature_3 Feature_correct
1        A     1       123       321       190              NA
2        A     1       123       321       190              NA
3        A     2       234       543       459              NA
4        A     3       345       654       392              NA
5        A     4       456       765       398              NA
6        A     4       456       765       398              NA
7        A     5       567       876       492              NA
8        A     6       678       987       587              NA
9        A     6       678       987       587              NA
10       B     1       831       912       761              NA
11       B     2       444       302       901              NA
12       B     2       444       302       901              NA
13       B     3       461       900       783              NA
14       B     4       921       555       312              NA
15       B     5       436       382       880              NA
16       B     5       436       382       880              NA
17       B     6       111       197       229              NA

我需要的是 Feature_correct 列包含来自 Feature_n 的值,具体取决于每个 SubjectTrial >。所以:

受试者 A 和试验 1 和 2:Feature_correct 包含 Feature_1 下受试者 A 和试验 1 和 2 的值(分别)。

受试者 A 和试验 3 和 4:Feature_correct 包含 Feature_2 下受试者 A 和试验 3 和 4 的值(分别)。

受试者 A 和试验 5 和 6:Feature_correct 包含 Feature_3(分别)下受试者 A 和试验 5 和 6 的值。

主题 B 依此类推。

这是我的目标:

df$Feature_goal <- c(rep(123, 2), 234, 654, rep(765, 2), 492, rep(587, 2), 831, rep(444, 2), 900, 555, rep(880, 2), 229)

head(df)
  Subject Trial Feature_1 Feature_2 Feature_3 Feature_correct Feature_goal
1       A     1       123       321       190              NA          123
2       A     1       123       321       190              NA          123
3       A     2       234       543       459              NA          234
4       A     3       345       654       392              NA          654
5       A     4       456       765       398              NA          765
6       A     4       456       765       398              NA          765

我知道如何手动执行此操作(在语法中指定主题名称和试用编号),但我想创建一个循环(或其他任何可行的方法),这样我就不必输入名称每个主题(在我的真实数据集中,我有很多参与者和许多“特征”变量)。

我试过这个 for 循环,但我得到一个错误:

df <- for(i in 1:nrow(df$Subject)) {
 if(df$Trial %in% c(1,2)){
   df[df$Subject == i $ df$Trial %in% c(1,2),]$Feature_correct = df[df$Subject == i & df$Trial %in% c(1,2),]$Feature_1
 }
  if(df$Trial %in% c(3,4)){
   df[df$Subject == i $ df$Trial %in% c(3,4),]$Feature_correct = df[df$Subject == i & df$Trial %in% c(3,4),]$Feature_2
  }
  if(df$Trial %in% c(5,6)){
   df[df$Subject == i $ df$Trial %in% c(5,6),]$Feature_correct = df[df$Subject == i & df$Trial %in% c(5,6),]$Feature_3
 }
}

> Error in 1:nrow(df$Subject) : argument of length 0

确实,

nrow(df$Subject)
> NULL

谁知道如何使它工作(使用循环或任何其他方式)?

最佳答案

矢量化的方法是通过粘贴带有 Trial 编号的“Feature”来创建行/列索引,以匹配它与原始数据帧中的列名和子集值。

df$Feature_Goal <- df[cbind(seq_len(nrow(df)), 
                      match(paste0("Feature_", df$Trial), names(df)))]
df

#   Subject Trial Feature_1 Feature_2 Feature_3 Feature_correct Feature_Goal
#1        A     1       123       321       190              NA          123
#2        A     1       123       321       190              NA          123
#3        A     2       234       543       459              NA          543
#4        A     2       234       543       459              NA          543
#5        A     3       345       654       392              NA          392
#6        A     3       345       654       392              NA          392
#7        B     1       456       765       398              NA          456
#8        B     1       456       765       398              NA          456
#9        B     2       567       876       492              NA          876
#10       B     2       567       876       492              NA          876
#11       B     3       678       987       587              NA          587
#12       B     3       678       987       587              NA          587

关于r - 使用来自其他列的某些行的值创建新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58802944/

相关文章:

r - 在substitute() 表达式中添加换行符

r - 具有交互作用的三因素逻辑回归

Python Pandas 使用列名称列表索引数据框

python - 删除pandas中重复的汉字

python - 扁平化 python 数据框中的条目,例如 Apache PIG 包

python - 如何在 Pandas 中使用 read_excel 提高处理速度?

r - 如何使用 ggplot2 绘制填充直方图及其密度?

r - geom_violin - "Error in grid.Call.graphics(C_lines, x$x, x$y, index, x$arrow) : invalid hex digit in ' 颜色'或 'lty'“

r - ggplot2 增加图例键之间的空间

r - 在R中找到两个数据帧之间的公共(public)ID