Combine multiple files with one header

I have five text files created by the R program (each of which has a header) that should be combined into one file. I combined them with rbind , and my problem is when I combine them, the resulting output has headers attached at the end of each file, for example, if the headers are assumed

 Combine Resultant file ABCD 1 3 5 7 ------------> Text file1 6 9 0 3 ABCD 1 3 6 7 ------------> Text file 2 5 7 8 3 and so on.... 

instead, I want the output file to have only one header in line 1 so the file should look like this:

 Combine Resultant file ABCD 1 3 5 7 ------------> Text file1 6 9 0 3 1 3 6 7 ------------> Text file 2 5 7 8 3 and so on.... 

Can someone tell me how to do this? The code I have is:

 S1 <- read.table("C:/Simulation_Results/sim_1_200.txt",sep=";",header= TRUE); S2 <- read.table("C:/Simulation_Results/sim_201_400.txt", sep=";",header= TRUE); S3 <- read.table("C:/Simulation_Results/sim_401_600_b.txt", sep=";",header= TRUE); S4 <- read.table("C:/Simulation_Results/sim_601_800.txt", sep=";",header= TRUE); S5 <- read.table("C:/Simulation_Results/sim_901_1000.txt",sep=";",header= TRUE); S6 <- read.table("C:/Simulation_Results/simulations_801_900.txt",sep=";",header= TRUE); options(max.print=28.5E6); S7 <- rbind(S1,S2,S3,S4,S5,S6) write.table(S7, file="C:/Simulation_Results/simulation_1_1000.txt", sep=";", row.names=TRUE, col.names=FALSE, quote = FALSE); 

Thanks!

0
source share
3 answers

It sounds like a wrong import. You must provide sample data. In any case, instead of read.table("some.file", header=TRUE, sep=";") , give read.csv2 try, since the default arguments are header=TRUE and sep=";" .

And instead of rbind , why don't you use merge ?

+1
source

Yes; There is a problem with your input that does not match your read function, and you are reading data with the wrong sep argument. I think you need to skip one line to go to the point where the headers are. Try the following:

 S1 <- read.table("C:/Simulation_Results/sim_1_200.txt", header= TRUE, skip=1) S2 <- read.table("C:/Simulation_Results/sim_201_400.txt", skip=1, header= TRUE) S3 <- read.table("C:/Simulation_Results/sim_401_600_b.txt", skip=1 , header= TRUE) S4 <- read.table("C:/Simulation_Results/sim_601_800.txt", skip=1, header= TRUE) S5 <- read.table("C:/Simulation_Results/sim_901_1000.txt", skip=1 , header= TRUE) S6 <- read.table("C:/Simulation_Results/simulations_801_900.txt", skip=1, header= TRUE) 

Then proceed as before. Sep = ";" discourages the separation of white space, and there was something in the leading line that caused your headers to not convert to column names.

0
source

after DWin: how about

 startvals <- c(1,201,401,601,801,901) endvals <- c(startvals[-1]-1,1000) fns <- paste("C:/Simulation_Results/",startvals,"_",endvals,".txt",sep="") ## I'm assuming for the moment that your S6 file is *not* named differently from the others S <- lapply(fns, read.csv2, skip=1) S_comb <- do.call(rbind,S) 

if you had your simulation files named in a specific way so that you can identify them all with list.files (pattern = "[some regular expression]"), then you could start this way ...

0
source

All Articles