[R] saving result of a "for" loop

Alex99 loyola9988 at yahoo.com
Thu Oct 16 16:57:19 CEST 2008

```Thanks a lot Stephen. It worked for me. also Jorge had a solution, it's a
short and easy code. U could have a look in case U want to use it in future.
Thanks again for all your help

stephen sefick wrote:
>
> a <- c(1:10)
> b <- c(.5, .6, .9, 10, .4, 3, 4, 9, 0, 11)
> d <- c(21:30)
>
> z <- data.frame(a,b,d)
> library(fields)
> results <- c()
> for(i in 1:(length(rownames(z))-1)){
>  results[i] <- rdist(z[i,], z[(i+1),])
>  }
>
> results.1 <- data.frame(results)
>  f <- rownames(z)
>  r <- f[-1]
>  rownames(results.1) <- r
>  colnames(results.1) <- f[1]
>
> This is a for loop that I used not too long ago defining the results
> outside of the loop worked for me.
> hope this helps
>
> stephen
>
> 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
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> --
> Stephen Sefick
> Research Scientist
>
> Let's not spend our time and resources thinking about things that are
> so little or so large that all they really do for us is puff us up and
> make us feel like gods.  We are mammals, and have not exhausted the
> annoying little problems of being mammals.
>
> 								-K. Mullis
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help