[R] loops in R

Steve Lianoglou lianoglou.steve at gene.com
Wed Nov 5 19:18:46 CET 2014


While you should definitely read the tutorial that Don is referring
to, I'd recommend you take a different approach and use more R
idiomatic code here.

In base R, this could be addressed with few approaches. Look for help
on the following functions:

  * tapply
  * by
  * aggregate

I'd rather recommend you also learn about some of the packages that
are better suited to deal with computing over data.frames,
particularly:

  * dplyr
  * data.table

You can certainly achieve what you want with for loops, but you'll
likely find that going this route will be more rewarding in the long
run.

HTH,
-steve


On Wed, Nov 5, 2014 at 10:02 AM, Don McKenzie <dmck at u.washington.edu> wrote:
> Have you read the tutorial that comes with the R distribution?  This is a very basic database calculation that you will
> encounter (or some slight variation of it) over and over.  The solution is a few lines of code, and someone may write it
> out for you, but if no one does
>
> You have 20 populations, so you will have 20 iterations in your for loop. For each one, you will need a unique identifier that points to
> the rows of "R" associated with that population. You'll calculate a mean and variance 20 times, and will need a data object to store
> those calculations.
>
> Look in the tutorial for syntax for identifying subsets of your data frame.
>
>> On Nov 5, 2014, at 5:41 AM, Noha Osman <nmo_138 at usc.edu> wrote:
>>
>> Hi Folks
>>
>> Iam  a new user of R and I have a question . Hopefully anyone help me in that issue
>>
>>
>> I have that dataset as following
>>
>> Sample      Population  Species  Tissue         R         G        B
>> 1 Bari1_062-1      Bari1     ret   seed  94.52303  80.70346 67.91760
>> 2 Bari1_062-2      Bari1     ret   seed  98.27683  82.68690 68.55485
>> 3 Bari1_062-3      Bari1     ret   seed 100.53170  86.56411 73.27528
>> 4 Bari1_062-4      Bari1     ret   seed  96.65940  84.09197 72.05974
>> 5 Bari1_062-5      Bari1     ret   seed 117.62474  98.49354 84.65656
>> 6 Bari1_063-1      Bari1     ret   seed 144.39547 113.76170 99.95633
>>
>> and I have 20 populations as following
>>
>> [1] Bari1      Bari2      Bari3      Besev      Cermik     Cudi       Derici     Destek     Egil
>> [10] Gunasan    Kalkan     Karabace   Kayatepe   Kesentas   Ortanca    Oyali      Cultivated Sarikaya
>> [19] Savur      Sirnak
>>
>> I need to calculate mean and variance of each population using column [R] using  for-loop
>>
>>
>> Thanks
>>
>>       [[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.
>
> Don McKenzie
> Research Ecologist
> Pacific Wildland Fire Sciences Lab
> US Forest Service
>
> Affiliate Faculty
> School of Environmental and Forest Sciences
> University of Washington
> dmck at uw.edu
>
> ______________________________________________
> 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.



-- 
Steve Lianoglou
Computational Biologist
Genentech



More information about the R-help mailing list