I have several excel files and need to combine the fourth column of each table. However, the rows of the table are different. Some of them have 7 rows but some of them have 5 or 6 rows. Here are my data descriptions.
As you can see, there are three excel files and I need to merge the fourth column. One of them has 7 rows while the other two has 5 and 6 rows. I try to use a loop reading all files inside a folder and then use "merge" to do this.
Here is my code.
rm(list=ls())
updir = "D:/STAR/MergeResults/Rtest"
library(readxl)
setwd(updir) #set up the working dictionary
outfile <- "" #first define an empty variable
file_list <- list.files(pattern = NULL)
for (i in 1:length(file_list)) {
file <- read_excel(file_list[i],col_names = TRUE)
variable <- file[,4]
outfile <-merge(outfile,variable)
}
Can anyone help me merge these columns with different rows?
I changed the way outfile
is initialized so that it is an empty vector. You can combine vectors using the c()
function. The rest of your code can remain the same.
cols <- apply(file_list,function(x) {
read_excel(file_list[i],col_names = TRUE)[,4]
})
max_length <- max(sapply(cols,length))
outfile <- data.frame(x = 1:max_length)
for(v in cols) {
vmod <- c(v,rep(NA,max_length - length(v)))
outfile <- cbind(outfile,vmod)
}
outfile <- outfile[,-1]
write to excel:
library(openxlsx)
write.xlsx(outfile, file = "NewFile.xlsx", colNames = TRUE,rowNames = FALSE)
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.