I have two columns that I want to collapse if they are different. If they are the same, just keep the value. If there is one NA, keep the other one.
df
# element1 element2
#1 A
#2 B B
#3 C D
#4 A <NA>
And I need the following column:
# element1 element2 element12
#1 A A
#2 B B B
#3 C D C,D
#4 A <NA> A
Any hint on how can I do that?
Data:
df = data.frame(element1 = c("A", "B", "C","A"),
element2 = c("", "B","D", NA));df