I have data like this:
x1 = seq(0, 2, length=5)
x2 = seq(1, 2, length=5)
x3 = seq(0, 1, length=5)
df = data.frame(rbind(x1,x2,x3))
I would like to obtain the proportion of specific columns (based on the name) that have a value less than 1. The following selects the variables that contain "x" in the name and sums across the values in the columns.
df <- df %>%
mutate(sumVar = rowSums(select(., contains("x")), na.rm = TRUE))
Is there a way to include ifelse logic within this setup to determine the proportion of columns with values < 1 (as opposed to calculating the sum as i have here)? I'm using the contains feature as I want to calculate this across a larger number of columns that are not necessarily in order, but have the same pattern in their name.