Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Expand comma separated columns on two columns while adding NA to repeated values in R

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading