开发者

Combine the multiple files with one header

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=";"), give read.csv2 a try, since it has header=TRUE and sep=";" as default arguments.

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


Yes; There is a problem with your input data not matching your read function and you are reading in data with the wrong sep argument. I think you need to skip one line to get down to the point where the headers are. Try this:

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. The sep=";" was preventing the white-space separation from being recognized and there was something in the leading line that was causing your headers not to be converted to column names.


following up on 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 particular way so that you could identify them all with list.files(pattern="[some regular expression]") then you could start that way ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜