0

これまでのところ、各行に5つしかありませんが、まもなく20を超えるので、これを行うためのより良い方法を見つける必要があります。ループについて考えforていましたが、名前(mt1、mt2など)の番号を反復処理する方法がわかりません。

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")
4

1 に答える 1

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で同じである ことに注意してください。

編集

この場合、ループオーバーすることをお勧めします。これにより、データ出力の長さが== 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")
})
于 2013-03-08T09:39:23.493 に答える