0

I am executing a map_df function that results in a dataframe similar to the df below.

name <- c('foo', 'foo', 'foo', 'bar', 'bar', 'bar')
year <- c(19, 19, 19, 18, 18, 18)
A <- c(1, NA, NA, 2, NA, NA)
B <- c(NA, 3, NA, NA, 4, NA)
C <- c(NA, NA, 2, NA, NA, 5)
df <- data.frame(name, year, A, B, C)

  name year  A  B  C
1  foo   19  1 NA NA
2  foo   19 NA  3 NA
3  foo   19 NA NA  2
4  bar   18  2 NA NA
5  bar   18 NA  4 NA
6  bar   18 NA NA  5

Based on a my unique groups within the df, in this case: name + year, I want to merge the data into the same row. Desired result:

  name year A B C
1  foo   19 1 3 2
2  bar   18 2 4 5

I can definitely accomplish this with a mix of filtering and joins, but with my actual dataframe that would be a lot of code and inefficient. I'm looking for a more elegant way to "squish" this dataframe.

TTS
  • 1,818
  • 7
  • 16

1 Answers1

2
library(dplyr)

df %>%
    group_by(name, year) %>%
    summarise_all(mean, na.rm = TRUE)

This is a dplyr answer. It works, if your data really looks like the one you posted.

Output:

  name   year     A     B     C
  <fct> <dbl> <dbl> <dbl> <dbl>
1 bar      18     2     4     5
2 foo      19     1     3     2
Georgery
  • 7,643
  • 1
  • 19
  • 52
  • Wow this is simple, and definitely a bit sneaky... but for my purposes I think this should work. Reviewing data now. – TTS Jan 30 '20 at 15:42