[R] how to use by() ?

Bert Gunter gunter.berton at gene.com
Mon Nov 29 19:42:38 CET 2010


... or slightly less verbose:

m1 <- within(m1,major_allele <-  ifelse(  Freq1 == MAF, Al1,  Al2 ))

?within

Cheers,
Bert

On Mon, Nov 29, 2010 at 10:25 AM, Greg Johnson <greg at nosnhoj.org> wrote:
> Jim Moon <moonja <at> ohsu.edu> writes:
>
>> How might one accomplish this using the by() function?
>> m1 is a data frame.
>>
>> # populate column "m1$major_allele"
>> for ( i in 1:length(m1$major_allele)) {
>>   if ( m1$Freq1[i] == m1$MAF[i]){
>>     m1$major_allele[i] = m1$Al1[i]
>>   }
>>   else{
>>      m1$major_allele[i] = m1$Al2[i]
>>   }
>> }
>
> You could use:
>
> m1$major_allele <- ifelse(  m1$Freq1 == m1$MAF, m1$Al1,  m1$Al2 )
>
> Greg
>
> ______________________________________________
> 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.
>



-- 
Bert Gunter
Genentech Nonclinical Biostatistics



More information about the R-help mailing list