我希望在这种情况下获得摘要:
如果NA给定值NA
df <- structure(list(id.Var1 = c("A", "A", "B", "A", "B", "C", "A",
"B", "C", "D"), id.Var2 = c("B", "C", "C", "D", "D", "D", "E",
"E", "E", "E"), Part1 = c(NA, 0, 0, 60, 100, 0, 20, 50, 20,
80), Part2 = c(NA, 60, 100, 0, 0, 100, 20, 50, 20, 0), Part3 = c(NA,
40, 0, 40, 0, 0, 60, 0, 60, 20)), row.names = c(37L, 73L, 74L,
109L, 110L, 111L, 145L, 146L, 147L, 148L), class = "data.frame")
答案 0 :(得分:2)
使用tidyverse
:
df%>%
mutate(res=case_when(
Part1>Part2 & Part1>Part3~1,
Part2>Part1 & Part2>Part3~-1,
Part3>Part1 & Part3>Part2~0,
Part3==Part2 | Part1==Part2 | Part1==Part3~0,
is.na(Part3) | is.na(Part2) | is.na(Part1)~NA_real_
))
id.Var1 id.Var2 Part1 Part2 Part3 res
1 A B NA NA NA NA
2 A C 0 60 40 -1
3 B C 0 100 0 -1
4 A D 60 0 40 1
5 B D 100 0 0 1
6 C D 0 100 0 -1
7 A E 20 20 60 0
8 B E 50 50 0 0
9 C E 20 20 60 0
10 D E 80 0 20 1
答案 1 :(得分:1)
某些条件似乎是多余的,例如如果Part1> Part2,则两者不能相等;如果Part1或Part2大于Part3(前两个条件),则Part3不能大于Part1或Part2(第三个条件)。所以你可以简单地做
with(df, (Part1 > Part2 & Part1 > Part3) * 1 +
(Part2 > Part1 & Part2 > Part3) * -1 )
# [1] NA -1 -1 1 1 -1 0 0 0 1
给出与jyjek的dplyr
代码相同的结果。