mjberlin15
mjberlin15

Reputation: 147

Summary column with conditional if in r

I want to have summary column with these conditions:

Code:

    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")

Upvotes: 0

Views: 516

Answers (2)

lebatsnok
lebatsnok

Reputation: 6459

Some of the conditions seem redundant, e.g. if Part1 > Part2 then the two cannot be equal, and if either Part1 or Part2 is greater than Part3 (first two conditions) then Part3 cannot be greater than Part1 or Part2 (third condition). So you might simply do

with(df, (Part1 > Part2 & Part1 > Part3) *  1 + 
         (Part2 > Part1 & Part2 > Part3) * -1 )
# [1] NA -1 -1  1  1 -1  0  0  0  1

Gives the same result as jyjek's dplyr code.

Upvotes: 1

jyjek
jyjek

Reputation: 2707

With 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

Upvotes: 2

Related Questions