[R] saving result of a "for" loop
Alex99
loyola9988 at yahoo.com
Thu Oct 16 16:55:00 CEST 2008
Jorge you are awesome,
This code is very efficient. it's exactly what I wanted to do and it's very
short and not complicated.
Thanks a lot
Jorge Ivan Velez wrote:
>
> Dear Alex,
> Is this what you want?
>
> my=read.table(textConnection("
> X8 X9 X10 X102 X110 X177 X283 X284 X286 X292 X297 X306 X308 X314
> 0 1 0 0 0 1 0 0 0 0 0 0 0 0
> 0 0 0 0 1 0 0 0 0 0 0 0 1 0
> 0 1 0 0 0 0 0 0 0 0 0 0 1 0
> 1 0 0 0 1 0 0 0 0 1 0 0 0
> 0"),header=TRUE)
> closeAllConnections()
> rownames(my)=paste('s',1:4,sep="")
>
> # Five samples
> res=replicate(5,my[,sample(colnames(my),5)],simplify=FALSE)
>
> # mean for each sample
> Means=do.call(rbind,lapply(res,function(x) rowMeans(x)))
> rownames(Means)=paste('sample',1:5,sep="")
> Means
>
> # Global mean for sample
> colMeans(Means)
>
>
> HTH,
>
> Jorge
>
>
> On Thu, Oct 16, 2008 at 9:10 AM, Alex99 <loyola9988 at yahoo.com> wrote:
>
>>
>>
>> Hi everyone,
>> I have dataset which I take random samples of it 5 times. each time I get
>> the mean for rows for each sample.
>> at the end I need to calculate the Average of all means for each sample
>> and
>> each row. to clear it up I give an example:
>> say this is my dataset.
>> X8 X9X10X12 X13 X14 X15 X16X17X18X19 X20 X21 X22
>> s1 0 1 0 0 0 1 0 0 0 0 0 0 0 0
>> s2 0 0 0 0 1 0 0 0 0 0 0 0 1 0
>> s3 0 1 0 0 0 0 0 0 0 0 0 0 1 0
>> s4 1 0 0 0 1 0 0 0 0 1 0 0 0 0
>>
>> I get a random sample and mean of row for that sample(5 times , but I
>> just
>> put 2 of them here to give you an idea)
>>
>> X12 X9 X10
>> s1 0 1 0
>> s2 0 0 0
>> s3 0 1 0
>> s4 0 0 0
>> s1 s2 s3 s4
>> 0.3333333 0.0000000 0.3333333 0.0000000
>>
>> X10 X18 X8
>> s1 0 0 0
>> s2 0 0 0
>> s3 0 0 0
>> s4 0 1 1
>> s1 s2 s3 s4
>> 0.0000000 0.0000000 0.0000000 0.6666667
>>
>> This is the code I used:
>> for(i in 1:5)
>> {
>> temp<-sample(A3,3, replace=F)
>> Avg=rowMeans(temp)
>> show(temp)
>> show(Avg)
>> }
>>
>> Now, the problem is how can I save the result for each row(s1,s2,s3,s4)
>> so
>> that I can get the grand average from 5 runs?I thought about using a
>> vector
>> in the "for" loop but it's no good, it over right so basically I only get
>> the means for last sample. any idea how to do it?
>> Thanks a lot
>>
>> --
>> View this message in context:
>> http://www.nabble.com/saving-result-of-a-%22for%22-loop-tp20013519p20013519.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org 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.
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.
>
>
--
View this message in context: http://www.nabble.com/saving-result-of-a-%22for%22-loop-tp20013519p20015092.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list