I have a dataset where some entries appear more than once, sometimes with data in one entry that is missing in another, sometimes with completely missing data, and sometimes with conflicting data:
# A tibble: 9 x 4
ID name age fsm
<chr> <chr> <dbl> <dbl>
1 0001 Peter 13 NA
2 0001 NA 13 1
3 0001 Barry 13 1
4 0002 Jane 13 1
5 0002 Jane NA 1
6 0003 Billy 12 0
7 0003 Billy 12 1
8 0004 Sally 12 NA
9 0004 Sally 12 NA
I would like to combine entries with missing data (e.g. 0001 and 0002), maintain conflicting data (0003) and maintain data that double NA (0004). To produce this:
# A tibble: 9 x 4
ID name age fsm
<chr> <chr> <dbl> <dbl>
1 0001 Peter 13 1
2 0001 Barry 13 1
3 0002 Jane 13 1
4 0003 Billy 12 0
5 0003 Billy 12 1
6 0004 Sally 12 NA
Building on this: how to combine repeated rows with missing fields R
I've written the following:
tmp %>%
group_by(ID) %>%
summarise(across(everything(), ~ ifelse(length(na.omit(.x)) == 0, NA, na.omit(.x)))) %>%
distinct()
But it loses the double entry, 0003
ID name age fsm
<chr> <chr> <dbl> <dbl>
1 0001 Peter 13 1
2 0002 Jane 13 1
3 0003 Billy 12 0
4 0004 Sally 12 NA
data as a dput:
structure(list(ID = c("0001", "0001", "0001", "0002", "0002",
"0003", "0003", "0004", "0004"), name = c("Peter", NA, "Barry",
"Jane", "Jane", "Billy", "Billy", "Sally", "Sally"), age = c(13,
13, 13, 13, NA, 12, 12, 12, 12), fsm = c(NA, 1, 1, 1, 1, 0, 1,
NA, NA)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -9L), spec = structure(list(cols = list(
ID = structure(list(), class = c("collector_character", "collector"
)), name = structure(list(), class = c("collector_character",
"collector")), age = structure(list(), class = c("collector_double",
"collector")), fsm = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))