Counting yes's in multiple columns

I would like to count the yes' in multiple columns to generate a new 'ever' yes variable. Data frame & mutate code below-

df_temp_wide2 <- data.frame(
 symptoms_fever_enrollment = c(yes,yes,no,no,no,yes),
 1= c(no,no,no,yes,yes),
2=c(yes,yes,yes,no,no)
3=c(yes,yes,yes,no,no)
4=c(no,no,no,no,no))


df_temp_wide2 %>%  #start with the data frame
  mutate(symptoms_fever_ever = case_when(symptoms_fever_enrollment == "yes" | `1`=="yes" | `2`=="yes" | `3`=="yes" | `4`=="yes") ~ "Yes", 
         symptoms_fever_ever = case_when(symptoms_fever_enrollment == "no" & `1`=="no" & `2`=="no" & `3`=="no" & `4`=="no") ~ "No")

The error I receive is:

Error: Problem with mutate() input symptoms_fever_ever.
x Input symptoms_fever_ever must be a vector, not a formula object.

i Input symptoms_fever_ever is ~....

You only need a single case_when in the mutate() function. I changed the definition of the data frame to fix some errors and to avoid numeric column names.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
df_temp_wide2 <- data.frame(
  symptoms_fever_enrollment = c("yes","yes","no","no","no"),
  `c1`= c("no","no","no","yes","no"),
  `c2`=c("yes","yes","yes","no","no"),
  `c3`=c("yes","yes","yes","no","no"),
  `c4`=c("no","no","no","no","no"))
df_temp_wide2 %>%  #start with the data frame
  mutate(symptoms_fever_ever = case_when(
    symptoms_fever_enrollment == "yes" | `c1`=="yes" | `c2`=="yes" | `c3`=="yes" | 
      `c4`=="yes" ~ "Yes", 
    symptoms_fever_enrollment == "no" & `c1`=="no" & `c2`=="no" & `c3`=="no" & 
      `c4`=="no" ~ "No"))
#>   symptoms_fever_enrollment  c1  c2  c3 c4 symptoms_fever_ever
#> 1                       yes  no yes yes no                 Yes
#> 2                       yes  no yes yes no                 Yes
#> 3                        no  no yes yes no                 Yes
#> 4                        no yes  no  no no                 Yes
#> 5                        no  no  no  no no                  No

Created on 2021-05-21 by the reprex package (v0.3.0)

DF <- data.frame(
  start = c(FALSE,TRUE,FALSE,FALSE,FALSE),
  time1 = c(FALSE,FALSE,FALSE,TRUE,TRUE),
  time2 = c(FALSE,TRUE,TRUE,FALSE,FALSE),
  time3 = c(FALSE,TRUE,TRUE,FALSE,FALSE),
  time4 = c(FALSE,FALSE,FALSE,FALSE,FALSE))

DF
#>   start time1 time2 time3 time4
#> 1 FALSE FALSE FALSE FALSE FALSE
#> 2  TRUE FALSE  TRUE  TRUE FALSE
#> 3 FALSE FALSE  TRUE  TRUE FALSE
#> 4 FALSE  TRUE FALSE FALSE FALSE
#> 5 FALSE  TRUE FALSE FALSE FALSE

DF["ever"] <- ifelse(rowSums(DF),TRUE,FALSE)

DF
#>   start time1 time2 time3 time4  ever
#> 1 FALSE FALSE FALSE FALSE FALSE FALSE
#> 2  TRUE FALSE  TRUE  TRUE FALSE  TRUE
#> 3 FALSE FALSE  TRUE  TRUE FALSE  TRUE
#> 4 FALSE  TRUE FALSE FALSE FALSE  TRUE
#> 5 FALSE  TRUE FALSE FALSE FALSE  TRUE

# if desired to convert to yes/no

ifelse(DF == TRUE,"yes","no")
#>      start time1 time2 time3 time4 ever 
#> [1,] "no"  "no"  "no"  "no"  "no"  "no" 
#> [2,] "yes" "no"  "yes" "yes" "no"  "yes"
#> [3,] "no"  "no"  "yes" "yes" "no"  "yes"
#> [4,] "no"  "yes" "no"  "no"  "no"  "yes"
#> [5,] "no"  "yes" "no"  "no"  "no"  "yes"

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.