簡體   English   中英

使用來自其他列的某些行的值創建新列

[英]Create new column with values from certain rows of other columns

我有一個 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 包含受試者 A 和試驗 1 和 2 在 Feature_1 下的值(分別)。

受試者 A 和試驗 3 和 4:Feature_correct 分別包含 Feature_2 下受試者 A 和試驗 3 和 4 的值。

受試者 A 和試驗 5 和 6:Feature_correct 包含受試者 A 和試驗 5 和 6 在 Feature_3 下的值(分別)。

對主題 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號的“功能”來創建行/列索引,以match其與原始 dataframe 中的列名和子集值匹配。

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

這是使用循環的解決方案。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM