Home > Software design >  tying to make a indicator based on count in R
tying to make a indicator based on count in R

Time:08-24

a   b   c   d   e   f   G
0   1   0   1   1   0   0
0   1   1   0   1   0   0
1   1   1   1   1   0   0
0   1   0   1   1   0   0
0   1   0   1   1   0   0

Consider this table I want to mutate a column with yes and no, "yes" if all columns(a:G) combined are greater than 3 and "no" if less than or equal to 3.

CodePudding user response:

How about:

library(dplyr)

table |> 
  mutate(sum_bigger_than_three = rowSums(across(a:G)) > 3)

Output:

# A tibble: 5 × 8
      a     b     c     d     e     f     G sum_bigger_than_three
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <lgl>                
1     0     1     0     1     1     0     0 FALSE                
2     0     1     1     0     1     0     0 FALSE                
3     1     1     1     1     1     0     0 TRUE                 
4     0     1     0     1     1     0     0 FALSE                
5     0     1     0     1     1     0     0 FALSE                
  • Related