I am trying to merge two rows by a similar group which I did by looking at different questions on stack overflow (Question1, Qestion2, Question3). All these questions stated what I want but I also have some empty fields in my data frame and I don't want to merge them. I only want to merge the similar/duplicate rows based on Col1
that contain values and not empty or NA
. I use below code but it also merges cells that are empty or NA
.
merge_my_rows <- df %>%
group_by(Col1) %>%
summarise(Col2 = paste(Col2, collapse = ","))
Below please is the sample df and Output df that I want.
Col1 | Col2 |
---|---|
F212 | ALICE |
D23 | John |
C64 | NA |
F212 | BOB |
C64 | NA |
D23 | JohnY |
D19 | Marquis |
Output df
Col1 | Col2 |
---|---|
F212 | ALICE, BOB |
D23 | John, JohnY |
C64 | NA |
C64 | NA |
D19 | Marquis |