I want to count the number of instances of some text (or factor level) row wise, across a subset of columns using dplyr.
Here's the input:
> input_df
num_col_1 num_col_2 text_col_1 text_col_2
1 1 4 yes yes
2 2 5 no yes
3 3 6 no <NA>
And here's the desired output:
> output_df
num_col_1 num_col_2 text_col_1 text_col_2 sum_yes
1 1 4 yes yes 2
2 2 5 no yes 1
3 3 6 no <NA> 0
In sum_yes we have counted the number of "yes" in that row.
I have tried two methods:
Attempted solution 1:
text_cols = c("text_col_1","text_col_2")
df = input_df %>% mutate(sum_yes = rowSums( select(text_cols) == "yes" ), na.rm = TRUE)
Errors with:
Error in mutate_impl(.data, dots) :
Evaluation error: no applicable method for 'select_' applied to an object of class "character".
Attempted solution 2:
text_cols = c("text_col_1","text_col_2")
df = input_df %>% select(text_cols) %>% rowsum("yes", na.rm = TRUE)
Errors with:
Error in rowsum.data.frame(., "yes", na.rm = TRUE) :
incorrect length for 'group'