I have a dataset where a bunch of character columns only have one value, the name of the column itself. Each row is an observation, and I want to count how many such columns exist for each row.
For example:
id multi_value_col single_value_col_1 single_value_col_2
1 A single_value_col_1
2 D2 single_value_col_1 single_value_col_2
3 Z6 single_value_col_2
What I'd like is add a column that counts how many of those single value columns there are per row. Like so:
id multi_value_col single_value_col_1 single_value_col_2 count
1 A single_value_col_1 1
2 D2 single_value_col_1 single_value_col_2 2
3 Z6 single_value_col_2 1
My initial idea was to use mutate_if
and n_distinct
, replacing the string by TRUE
, which could then be used in a mutate
with rowSums:
data %>%
mutate_if(~n_distinct(.) == 1, TRUE, .) %>%
mutate(count = rowSums???)
However, I can't get the mutate_if
working, and I'm not sure about the rowSums command either—is there a sum_if TRUE operating available across rows?