在 R 中使用匹配和应用

Ber*_*rdo 5 r match apply

> df = data.frame(id = 1:5, ch_1 = 11:15,ch_2= 10:14,selection = c(11,13,12,14,12))
> df
  id ch_1 ch_2 selection
1  1   11   10        11
2  2   12   11        13
3  3   13   12        12
4  4   14   13        14
5  5   15   14        12
Run Code Online (Sandbox Code Playgroud)

鉴于此数据集,我需要一个遵循规则的附加列:

  1. 如果选择是两个选择(ch_1 和 ch_2)之一,则返回选择的编号(1 或 2)
  2. 如果选择不是两个选择中的,则返回 3

我需要一种方法来为每一行执行此操作。对于单行,执行以下代码工作得很好,但我似乎无法找到一种方法来使用它来将它apply运行到数据帧的每一行。寻找一种不仅可以应用于更多的解决方案两列,运行速度比传统循环快

df=df[1,]

if (df$selection %in% df[,paste("ch_",1:2,sep="")]) {
  a = which(df[,paste("ch_",1:2,sep="")]==df$selection)
} else {
  a = 3
}
# OR
ifelse(df$selection %in% df[,paste("ch_",1:2,sep="")],1,3)
# OR
match(df$selection,df[,paste("ch_",1:2,sep="")])
Run Code Online (Sandbox Code Playgroud)

the*_*ail 6

将向量与其他列进行比较==,添加最后一列始终为TRUE,然后使用 获取TRUE每行中第一列的索引max.col

max.col(cbind(df$selection == df[c("ch_1","ch_2")], TRUE), "first")
#[1] 1 3 2 1 3
Run Code Online (Sandbox Code Playgroud)

这应该很容易扩展到n列。