johnzhou at fas.harvard.edu
2006-Jul-23 16:52 UTC
[R] Iterated Data Input/Output with Random Forests
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
For your last question of the 'paste', try tempdata[paste('pheno', i, sep='')] On 7/23/06, johnzhou@fas.harvard.edu <johnzhou@fas.harvard.edu> wrote:> > 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 > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]