简体   繁体   中英

Is there a way to automatically append data frame columns below each other into one column within large list of data frames?

I have a large list with thousands of data frames included in it. These data frames have multiple columns each. Thereby, I want to automatically bind in each of these data frames the columns into one column. This means that they are appended below each other as shown below. Thereafter, I would transform the list to a data frame which would have varying column lengths due to the different number of columns within each element in the original list.

From this:

y1 y2
1  4
2  5
3  6

To this:

y1
1
2
3
4
5
6

This should be done for each element in the list, whereby the solution needs to take into account that there are thousands of different data frames, which cannot be mentioned individually (example):

df1 = data.frame(
  X1 = c(1, 2, 3),
  X1.2 = c(4, 5, 6)
)
 df2 = data.frame(
  X2 = c(7, 8, 9),
  X2.2 = c(1, 4, 6)
)
df3 = data.frame(
  X3 = c(3, 4, 1),
  X3.2 = c(8, 3, 5),
  X3.3 = c(3, 1, 9)
)
 
listOfDataframe = list(df1, df2, df3)

Final output:

df_final = data.frame(
  X1 = c(1, 2, 3, 4, 5, 6),
  X2 = c(7, 8, 9, 1, 4, 6),
  X3 = c(3, 4, 1, 8, 3, 5, 3, 1, 9)
)

Another problem underlying this question is that there will be a differing number of rows, which I do not know how to account for in the data frame, as the columns need to have the same length.

Thank you in advance for your help, it is highly appreciated.

Structure of list within R: 列出带有数据框元素的 L

We can unlist after looping over the list with lapply

lst1 <- lapply(listOfDataframe, \(x)
   setNames(data.frame(unlist(x, use.names = FALSE)), names(x)[1]))

-output

lst1
[[1]]
  X1
1  1
2  2
3  3
4  4
5  5
6  6

[[2]]
  X2
1  7
2  8
3  9
4  1
5  4
6  6

[[3]]
  X3
1  3
2  4
3  1
4  8
5  3
6  5
7  3
8  1
9  9

If we need to convert the list to a single data.frame, use cbind.na from qPCR

do.call(qpcR:::cbind.na, lst1)
  X1 X2 X3
1  1  7  3
2  2  8  4
3  3  9  1
4  4  1  8
5  5  4  3
6  6  6  5
7 NA NA  3
8 NA NA  1
9 NA NA  9

Here is a tidyverse solution:

library(dplyr)
library(purrr)
listOfDataframe %>% 
  map(~.x %>% stack(.)) %>% 
  map(~.x %>% select(-ind))
[[1]]
  values
1      1
2      2
3      3
4      4
5      5
6      6

[[2]]
  values
1      7
2      8
3      9
4      1
5      4
6      6

[[3]]
  values
1      3
2      4
3      1
4      8
5      3
6      5
7      3
8      1
9      9

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM