J.Sabree
J.Sabree

Reputation: 2536

R count across columns based on conditional

I am trying to make a dataset that looks like this:

name    X1  X2  X3  Num_Low Num_0
case1   0.2 0.5 1   2        0
case2   1   1   1   0        0
case3   0.2 0.2 0   2        1
case4   0.5 1   1   1        0
case5   0   0   1   0        2
case6   0.2 0   0   1        2

Currently, my data set contains the name, X1, X2, and X3 columns. I need help creating the Num_Low and Num_0 columns.

Num_Low should be the number of the X variables for each row that have a value less than 1 but greater than 0.

Num_0 should be the number of the X variables that equal 0 exactly.

In my actual data set, I have many variables that all start with X, so if there is anything that I can do aside from typing X1, X2, etc, that'd be great (but isn't necessary!). If there's a way to do it with dplyr in a clear way, that'd also be super helpful!

Thanks!

Upvotes: 2

Views: 151

Answers (1)

DJack
DJack

Reputation: 4940

# Get the index of columns starting with "X" 
index <- which(substr(colnames(df), 1, 1) == "X")
# Compute the new variables based on your conditions
df$Num_Low <- rowSums(df[, index] < 1 & 
                      df[, index] > 0)
df$Num_0 <- rowSums(df[, index] == 0)
df

#   name  X1  X2 X3 Num_Low Num_0
#1 name1 0.2 0.5  1       2     0
#2 name2 1.0 1.0  1       0     0
#3 name3 0.2 0.2  0       2     1
#4 name4 0.5 1.0  1       1     0
#5 name5 0.0 0.0  1       0     2
#6 name6 0.2 0.0  0       1     2

dplyr version:

library(dplyr)
df %>%
  select(index) %>%
  mutate(Num_Low = rowSums(. < 1 & . > 0),
         Num_0 = rowSums(. == 0))

#   name  X1  X2 X3 Num_Low Num_0
#1 name1 0.2 0.5  1       2     0
#2 name2 1.0 1.0  1       0     0
#3 name3 0.2 0.2  0       2     1
#4 name4 0.5 1.0  1       1     0
#5 name5 0.0 0.0  1       0     2
#6 name6 0.2 0.0  0       1     2

Sample data

df <- data.frame(name = paste0("name", 1:6),
                 X1 = c(0.2,1,0.2,0.5,0,0.2),
                 X2 = c(0.5,1,0.2,1,0,0),
                 X3 = c(1,1,0,1,1,0))

Upvotes: 4

Related Questions