Hi,
I want collapse rows in columns Month
& Value
together while removing all NAs
library(dplyr)
dat <- structure(list(Group = c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3), Month = c("Jan",
"Feb", "Mar", "Jun", NA, NA, NA, NA, "Jan", "Mar", NA, NA, NA,
NA, NA, NA, "Feb", "Mar", "Jun", NA, NA, NA, NA, NA), Value = c(NA,
NA, NA, NA, "4", "5", "6", "4", NA, NA, NA, NA, "3", "2", NA,
NA, NA, NA, NA, NA, "8", "7", "4", NA)), row.names = c(NA, -24L
), class = c("tbl_df", "tbl", "data.frame"))
dat
#> # A tibble: 24 x 3
#> Group Month Value
#> <dbl> <chr> <chr>
#> 1 1 Jan <NA>
#> 2 1 Feb <NA>
#> 3 1 Mar <NA>
#> 4 1 Jun <NA>
#> 5 1 <NA> 4
#> 6 1 <NA> 5
#> 7 1 <NA> 6
#> 8 1 <NA> 4
#> 9 2 Jan <NA>
#> 10 2 Mar <NA>
#> # ... with 14 more rows
If I use coalesce_all_columns
function below, it only keeps one row for each unique Group
which is not what I'm looking for
coalesce_all_columns <- function(df) {
return(coalesce(!!! as.list(df)))
}
dat %>%
group_by(Group) %>%
summarise_all(coalesce_all_columns)
#> # A tibble: 3 x 3
#> Group Month Value
#> <dbl> <chr> <chr>
#> 1 1 Jan 4
#> 2 2 Jan 3
#> 3 3 Feb 8
I can get the desired output if I use data.table
package but I'm also interested in a dplyr
solution
library(data.table)
setDT(dat)[, lapply(.SD, na.omit), by = Group]
#> Group Month Value
#> 1: 1 Jan 4
#> 2: 1 Feb 5
#> 3: 1 Mar 6
#> 4: 1 Jun 4
#> 5: 2 Jan 3
#> 6: 2 Mar 2
#> 7: 3 Feb 8
#> 8: 3 Mar 7
#> 9: 3 Jun 4
Any help is appreciated. Thank you!