0

I would like to create a new column that counts the number of "yes" occurrences across a select number of variables (X1 - X3). Here is an example of my dataframe:

df <- data.frame(name = paste0("name", 1:6),
                 X1 = c("yes","no","yes","yes","yes","maybe"),
                 X2 = c("yes","yes","yes","maybe","yes","maybe"),
                 X3 = c("no","yes","yes","maybe","yes","yes"))

I want my new column to look like this:

count_yes = c(2,2,3,1,3,1)

df2 <- cbind(df,count_yes)

Thank you!!

hak515
  • 47
  • 5

1 Answers1

1

Using dplyr and stringr:

library(dplyr)
library(stringr)

df <- data.frame(name = paste0("name", 1:6),
                 X1 = c("yes","no","yes","yes","yes","maybe"),
                 X2 = c("yes","yes","yes","maybe","yes","maybe"),
                 X3 = c("no","yes","yes","maybe","yes","yes"))

df <- df %>%
  mutate(count_yes = str_count(X1, "yes") + str_count(X2, "yes") + str_count(X3, "yes"))

With output:

> df
   name    X1    X2    X3 count_yes
1 name1   yes   yes    no         2
2 name2    no   yes   yes         2
3 name3   yes   yes   yes         3
4 name4   yes maybe maybe         1
5 name5   yes   yes   yes         3
6 name6 maybe maybe   yes         1

UPDATE

df <- df %>%
  mutate(count = across(.cols = contains("X"), .fns = str_count, "yes")) %>%
  rowwise() %>%
  mutate(count_yes = across(.cols = contains("count"), .fns = sum)) %>%
  select(name, X1, X2, X3, count_yes)
Paul van Oppen
  • 1,443
  • 1
  • 9
  • 18
  • If the data has 100 columns, you need to type `str_count(X, "yes") + ...` 100 times. It's not a good programming idea. – Darren Tsai Aug 17 '20 at 08:36
  • You could also have clarified this in your question and ask how to approach this if there are many columns. See update, – Paul van Oppen Aug 17 '20 at 09:28