【问题标题】:Create new column with values from certain rows of other columns使用来自其他列的某些行的值创建新列
【发布时间】:2019-11-12 17:01:39
【问题描述】:

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

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。所以:

Subject A & Trials 1 和 2:Feature_correct 分别包含 Feature_1 下 Subject A 和 Trials 1 和 2 的值。

Subject A & Trials 3 and 4:Feature_correct 分别包含 Feature_2 下 Subject A 和 Trials 3 和 4 的值。

Subject A & Trials 5 and 6:Feature_correct 分别包含 Feature_3 下 Subject A 和 Trials 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

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

【问题讨论】:

    标签: r dataframe


    【解决方案1】:

    一种矢量化方法是通过将带有Trial 编号的“功能”粘贴到match 它以及原始数据帧中的列名和子集值来创建行/列索引。

    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
    

    【讨论】:

    • 非常感谢!!但是,对于我的真实数据集,这最终不起作用,因为我有 24 个试验对应于 4 个“feature_”列中的每一个。我的例子很不幸,试验次数和“feature_”匹配。我应该在问题中编辑我的示例吗? (堆栈溢出的新手,不知道我是否应该这样做)
    • @offrede 是的,请编辑您的示例以显示您的确切输入数据和预期输出。
    • 实际上,您的解决方案仍然适用于我原来的示例。我只需要创建一个新列 Trials2,其中 Trials %in% c(1:2) 被命名为“1”。然后,我使用 paste0("Feature_", df$Trial2)。谢谢! :)
    【解决方案2】:

    这是一个使用循环的解决方案。

        for (i in 1:3) {
        idx <- which(df$Trial == i)
        df[idx,6] <- df[idx,i+2]
        }
    

    【讨论】:

      猜你喜欢
      • 2021-05-12
      • 2016-11-07
      • 1970-01-01
      • 2019-12-29
      • 2016-11-17
      • 2019-04-02
      • 1970-01-01
      • 1970-01-01
      • 2023-01-09
      相关资源
      最近更新 更多