I have a data frame with several columns I want to merge only 5 of those.
ID | result___1 | result___2 | result___3 | result___4 | result___5 |
---|---|---|---|---|---|
1 | First | 0 | 0 | 0 | 0 |
3 | Second | 0 | First | 0 | 0 |
4 | First | 0 | First | 0 | 0 |
5 | Third | 0 | First | Second | 0 |
6 | Second | 0 | First | 0 | Other |
7 | Second | 0 | First | 0 | Other |
The merge of those will be in a new columne, seperated by "," if needed. For example for the first raw I want to keep only "First", for the second raw "Second, First" etc. My last column has text where it is different each time (because it was filled by interviewees). So the results it could be "Second, First, the interviewees text".
Following <https://stackoverflow.com/questions/50874955/merging-multiple-columns-in-a-dataframe-based-on-condition-in-r >
This is my code until now. But I don't get what I want
df %>% mutate_at(vars(starts_with("result___")), funs(na_if(.,"0"))) %>%
mutate(demo_race = coalesce(df$result___1,df$result___2,df$result___3,df$result___4,df$result___5)) %>%
select(ID, df$resultsnew)
Any hint how can I solve my problem?
Thank you in advance