[R] Iterated Data Input/Output with Random Forests

johnzhou at fas.harvard.edu johnzhou at fas.harvard.edu
Sun Jul 23 18:52:56 CEST 2006


Hi,

I am currently writing code to input a few thousand files, run them through the
Random Forests package, and then output corresponding results.

When I use the code below:

zz<-textConnection("ex.lm.out", "w")
sink(zz)
tempData<-read.delim(paste("allSnps",1,"Phenotype.phn",sep=""),header=TRUE,sep=",",quote="\"",dec=".")
tempData[[1]]<-factor(tempData[[1]])
tempData.rf<-randomForest(tempData[[1]]~.,data=tempData,importance=TRUE,proximity=TRUE,outscale=TRUE,replace=TRUE)
tempData.rf
zz<-file(paste("ex",1,".data",sep=""), "w")
cat(ex.lm.out, sep="\n", file=zz)
sink()
close(zz)

I am able to successfully input and output for one file. However, if I try to
use a for loop or a while statement e.g.

for(i in 1:2)
{
zz<-textConnection("ex.lm.out", "w")
sink(zz)
tempData<-read.delim(paste("allSnps",i,"Phenotype.phn",sep=""),header=TRUE,sep=",",quote="\"",dec=".")
tempData[[1]]<-factor(tempData[[1]])
tempData.rf<-randomForest(tempData[[1]]~.,data=tempData,importance=TRUE,proximity=TRUE,outscale=TRUE,replace=TRUE)
tempData.rf
zz<-file(paste("ex",i,".data",sep=""), "w")
cat(ex.lm.out, sep="\n", file=zz)
sink()
close(zz)
}

I get no error statements but the output is blank. Without the for statement,
setting i<-1 works fine.

One other related question is that right now I am trying to gett the loop to
work by using the paste() function with a variable (i). However, the paste
function returns a string.

If I wanted to make a loop of

tempData$pheno1
tempData$pheno2
tempData$pheno3
...

the paste() function will not work. Is there some other method to achieve the
desired effect? Thank you in advance! I have only been working with R for a few
days so please bear with my lack of knowledge!

John Zhou



More information about the R-help mailing list