I want to sum across multiple columns that have a particular pattern for the column name.
The following works:
sum = rowSums(across(matches('pattern')), na.rm = TRUE)
However, I want to only sum if the value is 1 or NA (0). So if the value is 2 for example, it will ignore it and essentially count it as a zero. Would the which() function help with this?
For example:
The total of 0,NA,1,1,1,0 would be 3
The total of 0,NA,2,3,1,NA would be 1
Thanks!
CodePudding user response:
For dummy data x
defined like
x <- data.frame(
v1 = c(0,NA,2,3,1,NA),
v2 = c(0,NA,1,1,1,0)
) %>% t
[,1] [,2] [,3] [,4] [,5] [,6]
v1 0 NA 2 3 1 NA
v2 0 NA 1 1 1 0
You may try rowSums(ifelse(x>1, 0, x), na.rm = T)
v1 v2
1 3
CodePudding user response:
If I have understood correctly, you only want to count number of 1's in columns with specific pattern which can be done with rowSums
as -
library(dplyr)
df <- data.frame(a1 = c(1, 0, 2, NA, 1), a2 = c(1, 3, 0, 4, NA))
df %>%
mutate(sum = rowSums(dplyr::select(., matches('a')) == 1, na.rm = TRUE))
# a1 a2 sum
#1 1 1 2
#2 0 3 0
#3 2 0 0
#4 NA 4 0
#5 1 NA 1