简体   繁体   中英

How can I create a single dataframe from a list of dataframes in R, when they have different number of rows?

I am trying to create a single dataframe from a list, but they have different number of rows.

List of 3 $:'data.frame': 210 obs. of 1 variable: ..$ protein: chr [1:210] "sp|A0A075B6R9|KVD24_HUMAN sp|A0A0C4DH68|KV224_HUMAN " "sp|O14791|APOL1_HUMAN " "sp|O75636|FCN3_HUMAN " "sp|P00450|CERU_HUMAN "... $:'data.frame': 210 obs. of 1 variable: ..$ protein: chr [1:210] "sp|A0A075B6R9|KVD24_HUMAN sp|A0A0C4DH68|KV224_HUMAN " "sp|O14791|APOL1_HUMAN " "sp|O75636|FCN3_HUMAN " "sp|P00450|CERU_HUMAN "... $:'data.frame': 215 obs. of 1 variable: ..$ protein: chr [1:215] "sp|A0A075B6R9|KVD24_HUMAN sp|A0A0C4DH68|KV224_HUMAN " "sp|O14791|APOL1_HUMAN " "sp|O75636|FCN3_HUMAN " "sp|P00450|CERU_HUMAN "...

How can I do this in R?

Thanks in advance!

Do you want to bind the rows? Here is a solution with base R using do.call and rbind and a tidyverse solution.

df1 <- data.frame(a = 1:3, b = 4:6)
df2 <- data.frame(a = 11:13, b = 14:16)

df_list <- list(df1, df2)

do.call(rbind, df_list)
#>    a  b
#> 1  1  4
#> 2  2  5
#> 3  3  6
#> 4 11 14
#> 5 12 15
#> 6 13 16

library(tidyverse)
bind_rows(df_list)
#>    a  b
#> 1  1  4
#> 2  2  5
#> 3  3  6
#> 4 11 14
#> 5 12 15
#> 6 13 16

Created on 2022-08-16 by the reprex package (v2.0.1)

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