按照特定顺序组合多个Excel文件中的列

我有12个Excel文件,每个文件有6列,259行。 一切都是数字,没有文字。 我必须制作一个72列和259行的表格,将12个文件组合在一起:

第一列应该是:

file 1 column 1, file 2 column 1, … file 6 column 1, file 1 column 2, file 2 column 2, … file 6 column 2, … file 1 column 6, file 2 column 6,…file 6 column 6 file 7 column 1, file 8 column 1, … file 12 column 1, file 7 column 2, file 8 column 2, … file 12 column 2, … file 12 column 6, file 12 column 6, … file 12 column 6 

在决赛桌上,我需要添加两个文本列和四个文本列。 我试过这种方法:

 M1<-cbind(file1[1],file2[1],file3[1],file4[1],file5[1],file6[1]) 

然后合并临时表,但是在使用cbind / rbind和merge手动写入所有内容时会变得很大。 我想知道我能否以更短的方式做到这一点。

有没有可能更容易做到这一点?

该脚本用于组合6个.txt文件或6个Excel文件中的列。 首先,我试着用6个文件来看看它是如何工作的,但是我必须使用最终forms(excel或.txt)中的12个文件。 看起来很可怕,因为我是R的初学者。还在学习。 感谢您的时间。

 rm(list=ls()) setwd("path_to_the_folder_where_my_files_are") ############ the files are declared MAM <- c("G","H","I","M","N") TAM <-c("a","b","c","d","e") Index1f <-c("a","b","c","d","e") Index2f <-c("a","b","c","d","e") Index3f <-c("a","b","c","d","e") Index4f <-c("a","b","c","d","e") Index5f <-c("a","b","c","d","e") Index6f <-c("a","b","c","d","e") file7 <-c("","","Text 1","","","") file7a <-c("","","Text 2","","","") file7b <-c("","","Text 3","","","") file7c <-c("","","Text 4","","","") file7d <-c("","","Text 5","","","") file7e <-c("","","Text 6","","","") file8 <-c("","","Text 7","","","") file9 <-c("G", "H","I", "M", "N", "E ") file10 <-c("","","Text 8 ","","","") ######### import data from the files for (i in 1:5) { TAM [i] <- paste(MAM[i],"2000.txt",sep="_") } # The end of the loop for the files import F1 <- read.table("G2000.txt", header=TRUE,sep="\t") F2 <- read.table("H2000.txt", header=TRUE,sep="\t") F3 <- read.table("I2000.txt", header=TRUE,sep="\t") F4 <- read.table("M2000.txt", header=TRUE,sep="\t") F5 <- read.table("N2000.txt", header=TRUE,sep="\t") F6 <- read.table("E2000.txt", header=TRUE,sep="\t") F11 <- read.table("Gnames.txt", header=TRUE,sep="\t") #compute the indicators through the combination of the different columns or rows Index1 <- cbind(file1[1],file2[1],file3[1],file4[1],file5[1],file6[1]) write.table(Index1, "Index1.txt", row.names=F, col.names=T, sep="\t") Index2 <- cbind(file1[2],file2[2],file3[2],file4[2],file5[2],file6[2]) write.table(Index2, "Index2.txt", row.names=F, col.names=T, sep="\t") Index3 <- cbind(file1[3],file2[3],file3[3],file4[3],file5[3],file6[3]) write.table(Index3, "Index3.txt", row.names=F, col.names=T, sep="\t") Index4 <- cbind(file1[4],file2[4],file3[4],file4[4],file5[4],file6[4]) write.table(Index4, "Index4.txt", row.names=F, col.names=T, sep="\t") Index5 <- cbind(file1[5],file2[5],file3[5],file4[5],file5[5],file6[5]) write.table(Index5, "Index5.txt", row.names=F, col.names=T, sep="\t") Index6 <- cbind(file1[6],file2[6],file3[6],file4[6],file5[6],file6[6]) write.table(Index6, "Index6.txt", row.names=F, col.names=T, sep="\t") Index1f <- rbind(file7, file8, file9, file10, Index1) # I added 3 rows at the beginning of the table write.table(Index1f, "Index1f.xls", row.names=F, col.names=F, sep="\t") Index2f <- rbind(file7a, file8, file9, file10, Index2) # I added 3 rows at the beginning of the table write.table(Index2f, "Index2f.xls", row.names=F, col.names=F, sep="\t") Index3f <- rbind(file7b, file8, file9, file10, Index3) # I added 3 rows at the beginning of the table write.table(Index3f, "Index3f.xls", row.names=F, col.names=F, sep="\t") Index4f <- rbind(file7c, file8, file9, file10, Index4) # I added 3 rows at the beginning of the table write.table(Index4f, "Index4f.xls", row.names=F, col.names=F, sep="\t") Index5f <- rbind(file7d, file8, file9, file10, Index5) # I added 3 rows at the beginning of the table write.table(Index5f, "Index5f.xls", row.names=F, col.names=F, sep="\t") Index6f <- rbind(file7e, file8, file9, file10, Index6) # I added 3 rows at the beginning of the table write.table(Index6f, "Index6f.xls", row.names=F, col.names=F, sep="\t") IndexA <- cbind(Index1f, Index2f) # I joined some index write.table(IndexA, "IndexA.xls", row.names=F, col.names=T, sep="\t") IndexB <- cbind(Index3f, Index4f) # I joined some index write.table(IndexB, "IndexB.xls", row.names=F, col.names=T, sep="\t") IndexC <- cbind(Index5f, Index6f) # I joined some index write.table(IndexC, "IndexC.xls", row.names=F, col.names=T, sep="\t") IndexD <- cbind(IndexA, IndexB) # I joined some index write.table(IndexD, "IndexD.xls", row.names=F, col.names=T, sep="\t") IndexE <- cbind(IndexD, IndexC) # I joined some index write.table(IndexE, "Firstpartofthetable.xls", row.names=F, col.names=T, sep="\t") 

从我的描述中可以理解,这应该工作:

  install.packages('readxl') library(readxl) #here i am assuming the folder only contains your 12 files setwd('path_to_a_folder_where_your_files_are') files <- list.files() #read all your tables into one big table, side by side res_tmp <- NA for(x in files){ tmp <- read_excel(x) res_tmp <- cbind(res_tmp, tmp) } res_tmp <- res_tmp[, -1] #rearrange columns according to your specification ind <- do.call(c, lapply(1 : 6, FUN = function(i) seq(i, 72, by = 6))) res <- res_tmp[, ind] 

res应该是你的259 x 72matrix。

目前还不清楚问题的第二部分是输出(添加文本的行和列)。