Output table from for loop

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Output table from for loop

bmoran
Hi, this might be basic but can't get it to work and it is hampering my R usage:

#the loop is checking variance of rows, and cutting out rows with var>numVec[i]
#I define outMat as object names I want to output to (does this make sense? how else
#can I define sequential numbered output?)
#numVec is numbers I use in the loop

head(Counts)

             AN1  AN2   AN3   AN4   var
GENE1      99     0     6      0      2360
GENE2    183   136   137    160   496
GENE3    301   199   233    187   1856

outMat<-paste("NewCounts", 1:5, sep="_") #names of
numVec<-c(100,200,500,1000,1500)
cutGenes<-function(x) {
        for (i in 1:5) {
                keep<- Counts$var<numVec[i] #gives logical vector
                keep<-Counts[keep=="TRUE",] #apply logical vector to count data (table)
                outMat[i]<-keep
}
}

#this all works fine if I define 'outMat[i]' as 'outMat' but then I can't use the loop! The loop seems to make #outMat[i] take the i element of 'keep' and only stores this.

#Help much appreciated, sorry if it is VERY basic, just a head scratcher for me.

Thanks,

Bruce.
Reply | Threaded
Open this post in threaded view
|

Re: Output table from for loop

Michael Weylandt
I'd do it like this:

nums = 10:30

OutList <- vector("list", length(nums)) # The key is that you need to
return in a list: your loop iterations were all operating on the same
object OutMat and accessing different elements thereof instead of
creating new objects

for(cn in seq_along(nums)) OutList[[i]] <- Counts[Counts$var < nums[cn], ]

Michael

On Wed, Dec 7, 2011 at 10:39 AM, bmoran <[hidden email]> wrote:

> Hi, this might be basic but can't get it to work and it is hampering my R
> usage:
>
> #the loop is checking variance of rows, and cutting out rows with
> var>numVec[i]
> #I define outMat as object names I want to output to (does this make sense?
> how else
> #can I define sequential numbered output?)
> #numVec is numbers I use in the loop
>
> head(Counts)
>
>             AN1  AN2   AN3   AN4   var
> GENE1      99     0     6      0      2360
> GENE2    183   136   137    160   496
> GENE3    301   199   233    187   1856
>
> outMat<-paste("NewCounts", 1:5, sep="_") #names of
> numVec<-c(100,200,500,1000,1500)
> cutGenes<-function(x) {
>        for (i in 1:5) {
>                keep<- Counts$var<numVec[i] #gives logical vector
>                keep<-Counts[keep=="TRUE",] #apply logical vector to count data (table)
>                outMat[i]<-keep
> }
> }
>
> #this all works fine if I define 'outMat[i]' as 'outMat' but then I can't
> use the loop! The loop seems to make #outMat[i] take the i element of 'keep'
> and only stores this.
>
> #Help much appreciated, sorry if it is VERY basic, just a head scratcher for
> me.
>
> Thanks,
>
> Bruce.
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Output-table-from-for-loop-tp4169386p4169386.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.