2010-11-20 8 views
0

R 프로그램에서 생성 된 다섯 개의 텍스트 파일 (각각 헤더가 있음)이 하나의 파일로 결합됩니다. 나는 rbind를 사용하여 결합하고 내 문제는 내가 그들을 결합 할 때, 결과 출력 예를 들어 각 파일의 끝 부분에 부착 된 헤더를 가지고, 는 헤더가 내가 출력 파일을 원하는 대신 그 하나의 헤더로 여러 파일 결합

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.... 

을 가정하는 경우입니다

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.... 

사람이 어떻게 그렇게 말해 줄 수 : 1 그래서 파일이 같아야 라인에 하나의 헤더가하는? 코드는 다음과 같습니다.

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); 

고마워요!

+1

이것은 나에게 맞는 것 같습니다. 어쩌면 다른 프로그램에서 생성 된 txt 파일에 오류가있을 수 있습니다. '머리 (S1)'과'꼬리 (S1)'을 볼 수 있을까요? 또한 질문에서 코드의 서식을 지정하는 것이 좋습니다 (코드 섹션을 강조 표시하고 코드 버튼을 누르십시오 - 1010이있는 코드 버튼을 누르십시오). –

+0

S1의 머리와 꼬리를 볼 수 있습니까? 문제가 쉼표와 소수점의 일부입니까? Dwin이 지적한 것처럼, txt 파일을 쓰는 R 코드는 아마 헤더를 잘못 형성하고있을 것이다. –

+0

파일을 생성하는 코드가 있습니까? 그렇다면 개별 변수 x1 <- as.data.frame (table1)에 테이블을 저장하는 것입니다. 그리고 나서 rbind() –

답변

1

부적절한 가져 오기와 같습니다. 예제 데이터를 제공해야합니다. 어쨌든 read.table("some.file", header=TRUE, sep=";")을 사용하는 대신 read.csv2을 시도해보십시오. 기본 인수로 header=TRUEsep=";"이 있으므로 시도해보십시오.

그리고 rbind 대신 merge을 사용하지 않으시겠습니까?

0

예; 읽기 기능과 일치하지 않는 입력 데이터에 문제가있어 잘못된 sep 인수로 데이터를 읽는 중입니다. 헤더가있는 지점까지 내려 가려면 한 줄 건너 뛰는 것이 좋습니다. 이것을 시도하십시오 :

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) 

그런 다음 이전과 같이 진행하십시오. sep = ";" white-space 분리가 인식되지 못하게하고 주요 선에 헤더가 열 이름으로 변환되지 않도록하는 뭔가가있었습니다.

0

DWIN에 후속 : 당신은 어떻게 list.files (패턴 = "[몇 가지 정규 표현식]"로 그들 모두를 식별 할 수 있도록이 시뮬레이션 파일이 특정 방식으로 명명 한 경우

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) 

에 대한) 그러면 그 길을 시작할 수 있습니다 ...

관련 문제