I have a data.frame, in this format:
A w x y z
0.23 1 NA NA NA
0.12 NA 2 NA NA
0.45 NA 2 NA NA
0.89 NA NA 3 NA
0.12 NA NA NA 4
And I want to collapse w:x:y:z into a single column, while removing NA's. Desired result:
A Comb
0.23 1
0.12 2
0.45 2
0.89 3
0.12 4
My approach so far is:
df %>% unite("Comb", w:x:y:z, na.rm=TRUE, remove=TRUE)
However, "Comb" is being populated with strings such as 1_NA_NA_NA
and NA_NA_NA_4
i.e. it is not removing the NA's. I've tried switching to character NA's, but that leads to bizarre and unpredictable results. What am I doing wrong?
I'd also like to be able to do this when the original data.frame is populated with strings (in place of the numbers). Is there a method for this?