I have five text files that are produced by an R program, (each of them having header) that are to be combined into a single file. I have combined them using rbind and my problem is when I combine them, the resultant output has headers attached at end of each file for example,
if the headers are supposed
Combine Resultant file
A B C D
1 3 5 7 ------------> Text file1
6 9 0 3
A B C D
1 3 6 7 ------------> Text file 2
5 7 8 3
and so on....
instead of that I want the output file to have only one header at line 1
so the file should look like:
Combine Resultant file
A B C D
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 anyone tell me how to do that?
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!
This sounds like an improper import. You should provide an example data. Anyway, instead of using
read.table("some.file", header=TRUE, sep=";"), giveread.csv2a try, since it hasheader=TRUEandsep=";"as default arguments.And instead
rbind, why don’t you usemerge?