Search code examples
rtidyversecontainsdplyrrowsum

R Tidyverse - Identify proportion of select columns meeting criteria


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.


Solution

  • You can use rowMeans() on the condition:

    library(dplyr)
    
    df %>% 
      mutate(propVar = rowMeans(select(., contains("x")) < 1))
    
       x1   x2   x3   propVar
    1 0.0 1.00 0.00 0.6666667
    2 0.5 1.25 0.25 0.6666667
    3 1.0 1.50 0.50 0.3333333
    4 1.5 1.75 0.75 0.3333333
    5 2.0 2.00 1.00 0.0000000