mercredi 10 février 2021

R if statement with multiple conditions and multiple outcomes

I wonder if there exists a function for the following. I have a dataframe as following

test <- data.frame(replicate(2,sample(0:100,1000,rep=TRUE)))
cutoff.X1 <- 20
cutoff.X2 <- 60
test
    X1 X2
1   63 79
2   68 76
3   23 67
4   21 48
5   78 84
6   35 47
7   34 20
8   24 36
9   32 41
10  92 90
11  77 20
12  21 54
13  90 81
14  69 43
15  70 30
16  56 31
17   1 74
18 100 71
19  72 36
20  88 55

What I want is an added column stating X1, X2, none or both, depending on whether the row in X1 is above cutoff.X1, the vaule of X2 is above cutoff.X2, both are not, or both are above the cutoffs. I would know how to do it using multiple If statements, but since the real code is a lot of data, I wonder if there is a way without processing more data than necessary.

Aucun commentaire:

Enregistrer un commentaire