'How to merge rows of a dataframe that have the same value in a column

I have a dataframe that I have obtained after converting a long format dataframe into a wide format dataframe using tidyr::spread()

The dataframe

orig_dataframe = data.frame(ID = c(1,1,2,2,3,3,4,4), Name = c("","a", "", "b", "", "c", "", "d"), Val1=(NA, 10,NA, 20, NA, 30,NA,40), Val2 = (100,NA, 200,NA, 300, NA, 400,NA))

looks like below

    ID    Name    Val1    Val2
1   1              NA      10
2   1      a       100     NA
3   2              NA      20
4   2      b       200     NA
5   3              NA      30
6   3      c       300     NA
7   4              NA      40
8   4      d       400     NA

I want to combine the rows with duplicate IDs so the dataframe looks like this:

    ID    Name    Val1    Val2
1   1      a       100      10
2   2      b       200      20
3   3      c       300      30
4   4      d       400      40

How can I do that?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source