Reputation: 1
folx. Long time listener, first-time caller. I have a df that looks like:
ID | thing_1 | thing_2 | thing_3 |
---|---|---|---|
A001 | NA | YES | NA |
A001 | YES | NA | NA |
A002 | NA | NA | YES |
A002 | NA | YES | NA |
A002 | YES | NA | YES |
A003 | NA | YES | NA |
-------- | -------- | -------- | -------- |
I want it to look like:
ID | thing_1 | thing_2 | thing_3 |
---|---|---|---|
A001 | YES | YES | NA |
A002 | YES | YES | YES |
A003 | NA | YES | NA |
-------- | -------- | -------- | -------- |
All of the resources I found about joining rows concatenate all the columns' data into one column separated by commas or else they dealt with adding numerical values into a sum. I am dealing with characters. How do I maintain separate columns but squash all of the data into one row per ID?
Thanks!
Upvotes: 0
Views: 38
Reputation: 19088
This is one approach with tidyverse
library(dplyr)
library(tidyr)
df %>%
group_by(ID) %>%
fill(-ID, .direction="updown") %>%
distinct() %>%
ungroup()
# A tibble: 3 × 4
ID thing_1 thing_2 thing_3
<chr> <chr> <chr> <chr>
1 A001 YES YES NA
2 A002 YES YES YES
3 A003 NA YES NA
df <- structure(list(ID = c("A001", "A001", "A002", "A002", "A002",
"A003"), thing_1 = c(NA, "YES", NA, NA, "YES", NA), thing_2 = c("YES",
NA, NA, "YES", NA, "YES"), thing_3 = c(NA, NA, "YES", NA, "YES",
NA)), class = "data.frame", row.names = c(NA, -6L))
Upvotes: 1