I am new to dplyr/tidyverse and I would like to sum rows of a dataset if the values in a given column(s) exceed a given value. For example given this dataframe,
a<-c(2,3,2,1,0)
b<-c(2,3,3,2,1)
z<-c(3,2,1,1,0)
data.abz <- data.frame(a,b,z)
data.abz
a b z
1 2 2 3
2 3 3 2
3 2 3 1
4 1 2 1
5 0 1 0
I would like to sum across the rows if the value in column a or b is greater than 1 and if the value at column z is greater than 0. If the condition is not satisfied the row sum is 0. For example, given the previous data frame, I would like to get the following,
a b z sum_values
1 2 2 3 7
2 3 3 2 8
3 2 3 1 6
4 1 2 1 3
5 0 1 0 0
The last two rows do not satisfy the condition and therefore they were assigned a value of 0. This is what I have done but I am sure there is a better way to achieve the same.
data.abz <- data.frame(a,b,z) %>%
mutate_at(vars(c(a,b)),
function(x) case_when(x < 2 ~ 0, TRUE~as.double(x)))%>%
mutate(sum_values = rowSums(.[1:3]))
Any more idiomatic and better ideas with R and dplyr
?