I have the following toy data frame.
toy.df <- data.frame(Name = c("group1", "group2", "group3", "group4", "group5", "group6", "group7"),
col1 = c("pos", "neg", "NA", "pos","neg", "NA", "pos"),
col2 = c("pos", "pos", "NA", "pos","neg","NA", "neg"),
col3 = c("pos", "NA", "pos", "NA", "neg", "neg", "neg"))
I would like to mutate a new column that check the values of all columns per row. If they are all "pos" or "NA" mutate "pos", if they are all "neg" or "NA" mutate "neg" and if they are "pos" or "neg" or "NA" mutate "both".
The new column looks as follows:
col4 <- c("pos", "both", "pos", "pos","neg", "neg","both")
Here is the final data frame:
Name col1 col2 col3 col4
group1 pos pos pos pos
group2 neg pos NA both
group3 NA NA pos pos
group4 pos pos NA pos
group5 neg neg neg neg
group6 NA NA neg neg
group7 pos neg neg both