2013-03-08 3 views
0

지금까지 각 행에 대해 5 개만 있었지만 곧 20 개가 넘을 것이므로 더 나은 방법을 찾아야합니다. for 루프를 생각하고 있었지만 이름 (mt1, mt2 등)의 숫자를 반복하는 방법을 모르겠습니다.R 루프 이름과 요소

mt1<- do.call(cbind, dataoutput[[1]]) 
mt2<- do.call(cbind, dataoutput[[2]]) 
mt3<- do.call(cbind, dataoutput[[3]]) 
mt4<- do.call(cbind, dataoutput[[4]]) 
mt5<- do.call(cbind, dataoutput[[5]]) 

rownames(mt1)[1:No.file]<-paste("file", 1:No.file, sep=" ") 
rownames(mt2)[1:No.file]<-paste("file", 1:No.file, sep=" ") 
rownames(mt3)[1:No.file]<-paste("file", 1:No.file, sep=" ") 
rownames(mt4)[1:No.file]<-paste("file", 1:No.file, sep=" ") 
rownames(mt5)[1:No.file]<-paste("file", 1:No.file, sep=" ") 

library(reshape) 
mt11<-melt(mt1, id.vars="Legend") 
mt21<-melt(mt2, id.vars="Legend") 
mt31<-melt(mt3, id.vars="Legend") 
mt41<-melt(mt4, id.vars="Legend") 
mt51<-melt(mt5, id.vars="Legend") 
+1

같은 오류를 방지합니다,를 반복 할 더 'dataoutput'? – Arun

답변

4

당신은 예를 들어, lapply을 사용할 수 있습니다 : 여기

lapply(1:5,function(i){ 
     mt <- do.call(cbind, dataoutput[[i]]) 
     rownames(mt)[1:No.file]<- paste("file", 1:No.file, sep=" ") 
     mt.melt <-melt(mt, id.vars="Legend") 
}) 

별도의 변수를하는 것보다 훨씬 더 data.frame의 목록을 얻을.

paste("file", 1:No.file, sep=" ")은 모든 data.frame에서 동일하다는 점에 유의하십시오.

이 경우 편집

는이 DataOutput 인터페이스의 길이가 == 0이있는 경우를 처리하고 예의 보여줄 수 subscript out of bounds

lapply(dataoutput,function(do){ 
     mt <- do.call(cbind, do) 
     rownames(mt)[1:No.file]<- paste("file", 1:No.file, sep=" ") 
     mt.melt <-melt(mt, id.vars="Legend") 
}) 
+0

'i'는 한 번만 사용되기 때문에'dataoutput'에'lapply' 루프를 만들 수 있습니다. 'dataoutput'의 길이가 0 일 때 정상적으로 처리되는 경우도 처리합니다. – flodel

+0

@ flodel 의견에 따라 내 게시물을 편집합니다. merci beaucoup :) – agstudy