Home > other >  Counting number of selected columns with valid (not missing) values (R)
Counting number of selected columns with valid (not missing) values (R)

Time:09-03

I want to create a column that counts the cases where the value is not missing in the selected columns.

Here is the code:

a <- c("aa", "bb", "cc", "dd", "", NA)
b <- c("", NA, "aa", "", "", "dd")
c <- c("aa", "", NA, NA, "cc", "dd")
d <- c("aa", "bb", "", NA, "cc", "dd")
data <- data.frame(cbind(a,b,c,d))

data$cases <- rowSums(nchar(as.matrix(data))>1, na.rm = TRUE)

I would like to take only columns "b" and "c" into account. How do I complete the code?

CodePudding user response:

a <- c("aa", "bb", "cc", "dd", "", NA)
b <- c("", NA, "aa", "", "", "dd")
c <- c("aa", "", NA, NA, "cc", "dd")
d <- c("aa", "bb", "", NA, "cc", "dd")
data <- data.frame(cbind(a,b,c,d))

library(dplyr, warn = FALSE)
library(purrr)

data %>% 
  mutate(cases = across(c(b, c), \(x) !(is.na(x) | x == "")) %>% reduce(` `))
#>      a    b    c    d cases
#> 1   aa        aa   aa     1
#> 2   bb <NA>        bb     0
#> 3   cc   aa <NA>          1
#> 4   dd      <NA> <NA>     0
#> 5             cc   cc     1
#> 6 <NA>   dd   dd   dd     2

Created on 2022-09-02 with reprex v2.0.2

  •  Tags:  
  • r
  • Related