ddamko
ddamko

Reputation: 33

How to combine rows in a data frame with dplyr group by and flatten other data?

This is my data frame:

UID               HH0  HH1  HH2  HH3  HH4  HH5
JEZAF000080843824 NA   NA   NA   1    NA   NA
JEZAF000080843824 NA   NA   NA   NA   Y    NA
JEZAF000080843824 NA   NA   NA   NA   NA   2

Here is what I looking for in the results

UID               HH0  HH1  HH2  HH3  HH4  HH5
JEZAF000080843824 NA   NA   NA   1    Y    2

I have played around with dplyr with group_by but I am not sure what function to %>% into. I know there is probably a simple answer but I am new with R.

Upvotes: 3

Views: 1889

Answers (1)

akrun
akrun

Reputation: 887118

We can do this with na.omit

df1 %>%
     group_by(UID) %>%
     summarise_each(funs(na.omit))
# A tibble: 1 × 7
#               UID   HH0   HH1   HH2   HH3   HH4   HH5
#              <chr> <lgl> <lgl> <lgl> <int> <chr> <int>
#1 JEZAF000080843824    NA    NA    NA     1     Y     2

Upvotes: 2

Related Questions