I have the following dataframe:
structure(list(A = c("1,2,3", "1,2", "1,4"), B = c("X,Y", "X",
"X"), ID = c(1, 2, 3), D = c(1, 2, 3)), class = "data.frame", row.names = c(NA,
-3L))
I’m trying to expand A and B but creating NAs so no repeated values are found for columns. So columns A and B should expand "independently", as if the values of A and B are not directly correlated, they are only correlated to ID and D
structure(list(A = c("1", "2", "3", "NA", "NA", "1", "2", "NA",
"1", "4", "NA"), B = c("NA", "NA", "NA", "X", "Y", "NA", "NA",
"X", "NA", "NA", "X"), ID = c(1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3
), D = c(1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3)), class = "data.frame", row.names = c(NA,
-11L))
None of previous questions about expanding multiple columns that I could find did something similar!
>Solution :
This is a very unusual expansion. You could tackle each column separately and then join them. For example
library(dplyr)
library(tidyr)
bind_rows(
separate_longer_delim(select(dd, -B), A, ","),
separate_longer_delim(select(dd, -A), B, ",")
) |>
select(A,B,ID, D) |>
arrange(ID, D)
# A B ID D
# 1 1 <NA> 1 1
# 2 2 <NA> 1 1
# 3 3 <NA> 1 1
# 4 <NA> X 1 1
# 5 <NA> Y 1 1
# 6 1 <NA> 2 2
# 7 2 <NA> 2 2
# 8 <NA> X 2 2
# 9 1 <NA> 3 3
# 10 4 <NA> 3 3
# 11 <NA> X 3 3