I am attempting to group_by and filter a table, and eventually, mutate a new column based on a specific string value.
The table I have looks like this-
And what i’m attempting to do here is to mutate a new column that populates “1” based on the specific value, but for all the same IDs. This was i’m able to filter out all the id’s that have a specific value. Say that value is “gender”, I would like the new column values and table to be-
The way I have attempted this in the past is that I’m able to mutate a new column and do a group_by if there is n_distinct == 1, for example
new_table <- table %>% group_by(id) %>% mutate(result = if_else(value=="gender" & n_distinct(value)>1,"1",""))
But this wont work since id 1 also has
Also open to completely changing my method for something that works,