有条件地将值从一个数据帧映射到另一个数据帧

时间:2017-01-01 11:48:33

标签: r for-loop dataframe mapping

我有两个数据帧:

> SubObj

sNumber runningTrialNo  wordTar      SubObj_ind

1       34              nerd         3
1       32              hooligan     1
1       7               villager     3
2       32              oak          2
2       8               deer         2
3       8               mammal       3

> df

sNumber runningTrialNo  wordTar

1       34              nerd
1       34              nerd
1       34              nerd
1       32              hooligan
1       32              hooligan
1       7               villager
2       32              oak
2       32              oak
2       8               deer
3       8               mammal
3       8               mammal

我想将SubObj $ SubObj_ind中的值映射到df $ SubObj,因此所有值都将与sNumber(主题编号)和runningTrialNo(试用编号)一致。看起来应该像这样:

> df

sNumber runningTrialNo  wordTar    SubObj_ind

1       34              nerd       3
1       34              nerd       3
1       34              nerd       3
1       32              hooligan   1
1       32              hooligan   1
1       7               villager   3
2       32              oak        2
2       32              oak        2
2       8               deer       2
3       8               mammal     3
3       8               mammal     3

我编写的代码假设应该完成这项工作,但它没有映射试验和主题编号:

 df$SubObj_indO <- array(0, nrow(df))
 for(i in 1:nrow(SubObj)) {
    index <- df$runningTrialNo == SubObj[i,"runningTrialNo"] &
       df$sNumber == SubObj[i,"sNumber"]
    df$SubObj_ind[index] <- SubObj[index, "SubObj_ind"]
 }

代码的和平有什么问题?

1 个答案:

答案 0 :(得分:0)

我们可以使用match

df$SubObj_ind <- with(df, SubObj$SubObj_ind[match(wordTar, SubObj$wordTar)])
df
#     sNumber runningTrialNo  wordTar SubObj_ind
#1        1             34     nerd          3
#2        1             34     nerd          3
#3        1             34     nerd          3
#4        1             32 hooligan          1
#5        1             32 hooligan          1
#6        1              7 villager          3
#7        2             32      oak          2
#8        2             32      oak          2
#9        2              8     deer          2
#10       3              8   mammal          3
#11       3              8   mammal          3

或使用data.table

library(data.table)
setDT(df)[SubObj[c("wordTar", "SubObj_ind")], on = "wordTar"]