[英]Nested Match Functions in R
This is a follow up to How to lookup and sum multiple columns in R :这是对如何在 R 中查找和求和多个列的后续操作:
I have 3 data frames as such:我有3个数据框:
Groups:团体:
P1 P2 P3 P4
"Joe" "Sally" "A.J." "Mary"
"Cory" "Joe" "Sally" "Katy"
Names:名称:
ID NAME
123 "Joe"
213 "Sally"
312 "A.J."
231 "Mary"
345 "Cory"
567 "Katy"
Individual_Results:个人_结果:
ID SCORE
123 23
213 12
312 11
231 19
345 10
567 22
My goal is to create a new column in Groups
with a SCORE
column that is a sum of each of the results in the group我的目标是在
Groups
中创建一个新列,其中SCORE
列是组中每个结果的总和
P1 P2 P3 P4 SCORE
"Joe" "Sally" "A.J." "Mary" 65
Following the example of the answer in the referenced question above, I've attempted the following按照上面引用的问题中的答案示例,我尝试了以下操作
groups$score = apply(groups, 1, function(x){
sum(Individual_Results$SCORE[match(match(x, Names$Name), Individual_Results$ID)])
})
Unfortunately the result is creating the new column, but the result is NA
for every score.不幸的是,结果是创建新列,但每个分数的结果都是
NA
。
If I am understanding how to use both the apply
and match
correctly, what I am trying to do is apply
the function to each row, passing x
(the Name
) as the parameter to the first match
function in order to get the ID
and then matching the returned ID
to the second match
to get the score - summing all the scores per row.如果我了解如何正确使用
apply
和match
,我要做的是apply
function 应用到每一行,将x
( Name
)作为参数传递给第一个match
function 以获取ID
,然后将返回的ID
与第二个匹配项match
以获得分数 - 将每行的所有分数相加。
I think I am super close, but just not quite there.我想我非常接近,但并不完全在那里。 Appreciate any help!
感谢任何帮助!
This will do the trick.这会成功的。
Note that you don't need data.table
.请注意,您不需要
data.table
。 I just used it to make the example reproducible我只是用它来使示例可重现
require(data.table)
Groups <- data.frame(fread('"P1","P2","P3","P4"
"Joe","Sally","A.J.","Mary"
"Cory","Joe","Sally","Katy"'))
Names <- data.frame(fread('ID,NAME
123,"Joe"
213,"Sally"
312,"A.J."
231,"Mary"
345,"Cory"
567,"Katy"'))
Individual_Results <- data.frame(fread('ID,SCORE
123,23
213,12
312,11
231,19
345,10
567,22'))
Groups$SCORE <- apply(Groups, 1, function(x){
sum(Individual_Results$SCORE[match(Names$ID[match(x, Names$NAME)], Individual_Results$ID)])
})
# Inspect groups:
Groups
# P1 P2 P3 P4 SCORE
# 1 Joe Sally A.J. Mary 65
# 2 Cory Joe Sally Katy 67
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.