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

Merge dataframes with same name within a list when dataframe lengths differ

I have a list that contains several dataframes. My goal is to merge the dataframes that share the same name together. However, although two dataframes may share the same name, they do not always share the same number of rows. I followed this SO post until I encountered my issue below. I have included a reprex below that is an extension of this issue.

library(tidyverse)

d1 <- data.frame(id = "111", y1 = c(1, 2, 3), y2 = c(4, 2, 6))
d2 <- data.frame(id = "111" , y1 = c(2, 1), y2 = c(4, 4)) # missing a row
d3 <- data.frame(id = "222", y1 = c(2, 2, 3), y2 = c(2, 2, 6))
d4 <- data.frame(id = "222" , y1 = c(3, 2), y2 = c(2, 4)) # missing a row
d5 <- data.frame(id = "333", y1 = c(5, 5, 3), y2 = c(0, 2, 6))
d6 <- data.frame(id = "333" , y1 = c(3, 6, 7), y2 = c(1, 9, 4))

lst <- list(d1, d2, d3, d4, d5, d6)
rm(d1, d2, d3, d4, d5, d6)
names(lst) <- c("111", "111", "222", "222", "333", "333")
lstdf <- map(split(lst, names(lst)), bind_cols)
#> Error in `.f()`:
#> ! Can't recycle `..1` (size 3) to match `..2` (size 2).

Using the do.call(cbind, x) option causes a similar issue. My goal is to "cut off" the extra row and have the minimum number of rows for any ID number (if the ID number contains differing number of rows between its two dataframes). Any help is appreciated.

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 :

We may use a function that pads NA when the lengths are not same i.e. cbind.na from qpcR does it

library(purrr)
 map(split(lst, names(lst)),  ~ reduce(.x, qpcR:::cbind.na))
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