我想使用R读取并合并多个(> 20).xlsx文件。
这是我的.xlsx文件的外观。
Sample X Y Z
Sample1 5 3 1
Sample2 4 1 11
Sample3 9 11 9
Sample A B Z
Sample4 12 1 1
Sample5 6 1.1 1.41
Sample6 7 91 1
Sample C A Z
Sample7 4 2 9
Sample8 98 11 61
我想将它们与标题“样本”,“ X”,“ Y”,“ Z”,“ A”,“ B”,“ C”合并为单个df。没有特殊顺序,但应包含所有内容。
我发现了一些解决方案,它们可以处理具有相同结构(例如Merge multiple Excel files starting at row in R)的.xlsx文件,但是它们不能解决我的问题。
答案 0 :(得分:1)
这可以通过dplyr::bind_rows
轻松实现,与rbind
相比,它允许按行绑定具有不同列数和列名的df:
df1 <- read.table(text = "Sample X Y Z
Sample1 5 3 1
Sample2 4 1 11
Sample3 9 11 9", header = TRUE)
df2 <- read.table(text = "Sample A B Z
Sample4 12 1 1
Sample5 6 1.1 1.41
Sample6 7 91 1", header = TRUE)
df3 <- read.table(text = "Sample C A Z
Sample7 4 2 9
Sample8 98 11 61", header = TRUE)
df <- list(df1, df2, df3)
dplyr::bind_rows(df)
#> Sample X Y Z A B C
#> 1 Sample1 5 3 1.00 NA NA NA
#> 2 Sample2 4 1 11.00 NA NA NA
#> 3 Sample3 9 11 9.00 NA NA NA
#> 4 Sample4 NA NA 1.00 12 1.0 NA
#> 5 Sample5 NA NA 1.41 6 1.1 NA
#> 6 Sample6 NA NA 1.00 7 91.0 NA
#> 7 Sample7 NA NA 9.00 2 NA 4
#> 8 Sample8 NA NA 61.00 11 NA 98
答案 1 :(得分:1)
感谢@RonakShah和@Wimpel,它的工作原理是:
#path to files
path <- "C:/Users/your/path"
#list diles
filenames_list <- list.files(path= path, full.names=TRUE)
#Store files in a list
All <- lapply(filenames_list,function(filename){
read.xlsx(filename)
})
#merge the dfs
merged_df <- data.table::rbindlist(All, use.names = TRUE, fill = TRUE)