I want to apply an if else statement to each column (a-d) in a data frame where if each value satisfies a condition based on another column (m), the value stays the same, otherwise the value is changed to zero.
Here is a reproducible example:
a <- c(10, 11, 12, 15,7,8)
b <- c(15,11,16,17,1.5,9)
c <- c(18,14,6,14,1,17)
d <- c(12,11,13,19,2,4)
p <- c(2,1,3,2.5,1.3,2.1)
m <- c(12,13,8,9,14,11)
df<- data.frame(a,b,c,d,p,m)
print(df)
a b c d p m
1 10 15.0 18 12 2.0 12
2 11 11.0 14 11 1.0 13
3 12 16.0 6 13 3.0 8
4 15 17.0 14 19 2.5 9
5 7 1.5 1 2 1.3 14
6 8 9.0 17 4 2.1 11
This is the 'long' version of what I want to do which works but is very repetitive:
df$a<- ifelse((df$a-df$p)>df$m, df$a, 0)
df$b<- ifelse((df$b-df$p)>df$m, df$b, 0)
df$c<- ifelse((df$c-df$p)>df$m, df$c, 0)
df$d<- ifelse((df$d-df$p)>df$m, df$d, 0)
print(df)
This is the output I want:
a b c d p m
1 0 15 18 0 2.0 12
2 11 11 14 11 1.0 7
3 12 16 0 13 3.0 8
4 15 17 14 19 2.5 9
5 0 0 0 0 1.3 14
6 0 0 17 0 2.1 11
We can use an lapply
df[1:4] <- lapply(df[1:4], function(x) ifelse((x - df$p) > df$m, x, 0))
df
# a b c d p m
#1 0 15 18 0 2.0 12
#2 0 0 0 0 1.0 13
#3 12 16 0 13 3.0 8
#4 15 17 14 19 2.5 9
#5 0 0 0 0 1.3 14
#6 0 0 17 0 2.1 11
Or without using ifelse
df[1:4] <- ((df[1:4]-df$p) > df$m)*df[1:4]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With