Search code examples
rdplyrsparse-matrixplyrsummarize

Collapsing Sparse dataframe in R based on Group By


I have a df that looks like this:

Name Var0 Var1 Var2 Var3 Var4
A 0.1 NA NA NA NA
A NA 0.3 NA NA NA
A NA NA 0.4 NA NA
A NA NA NA 0.7 NA
A NA NA NA NA 0.9
B 0.2 NA NA NA NA
B NA 0.5 NA NA NA
B NA NA 0.8 NA NA
B NA NA NA 0.1 NA
B NA NA NA NA 0.3

It's essentially a sparse matrix that is grouped by the first column "Name". How can I collapse the rows so that there is only one row per Name? I've tried multiple solutions, including groupby and summarize functions, but can't find a good way to collapse the matrix.

Output I want:

 Name Var0 Var1 Var2 Var3 Var4
 A 0.1 0.3 0.4 0.7 0.9
 B 0.2 0.5 0.8 0.1 0.3

Solution

  • This should do it

    df %>% group_by(Name) %>% summarise_all(funs(na.omit(.)[1]))