[R] FW: how to use by() ?

William Dunlap wdunlap at tibco.com
Mon Nov 29 19:45:42 CET 2010


ifelse(cond,ifTrue,ifFalse) doesn't do what you
want when ifTrue or ifElse is a factor.
You can use as.character on the factors
  > with(m1, ifelse(Freq1==MAF, as.character(Al2), as.character(Al1)))
  [1] "G" "G" "C" "C"
or use the stringsAsFactors=FALSE argument to
data.frame (or read.table) when you make the
data.frame.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Jim Moon
> Sent: Monday, November 29, 2010 10:37 AM
> To: r-help at r-project.org
> Subject: [R] FW: how to use by() ?
> 
> Thank you for the suggestion, Bill.  The result is not quite 
> what I would like.  Here's sample code for you or anyone else 
> who may be interested:
> 
> Al1 = c('A','C','C','C')
> Al2 = c('G','G','G','T')
> Freq1 = c(0.0078,0.0567,0.9434,0.9908)
> MAF = c(0.0078,0.0567,0.0566,0.0092)
> m1 = data.frame(Al1=Al1, 
> Al2=Al2,Freq1=Freq1,MAF=MAF,major_allele='')  
> m1
> 
> Al1 Al2  Freq1    MAF major_allele
> 1   A   G 0.0078 0.0078            
> 2   C   G 0.0567 0.0567            
> 3   C   G 0.9434 0.0566            
> 4   C   T 0.9908 0.0092            
> 
> 
> Using the suggestion involving "with()" (I swapped Al1 and 
> Al2 from before, but this does not affect the nature of the output):
> 
> m1$major_allele <- with(m1, ifelse(Freq1==MAF, Al2, Al1));m1
> 
>   Al1 Al2  Freq1    MAF major_allele
> 1   A   G 0.0078 0.0078            1
> 2   C   G 0.0567 0.0567            1
> 3   C   G 0.9434 0.0566            2
> 4   C   T 0.9908 0.0092            2
> 
> 
> The output I desire is:
>   Al1 Al2  Freq1    MAF major_allele
> 1   A   G 0.0078 0.0078            G
> 2   C   G 0.0567 0.0567            G
> 3   C   G 0.9434 0.0566            C
> 4   C   T 0.9908 0.0092            C
> 
> 
> Jim
> 
> 
> -----Original Message-----
> From: William Dunlap [mailto:wdunlap at tibco.com] 
> Sent: Monday, November 29, 2010 10:02 AM
> To: Jim Moon
> Subject: RE: [R] how to use by() ?
> 
> m1$major_allele <- with(m1, ifelse(Freq1==MAF, Al1, Al2))
> 
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com  
> 
> > -----Original Message-----
> > From: r-help-bounces at r-project.org 
> > [mailto:r-help-bounces at r-project.org] On Behalf Of Jim Moon
> > Sent: Monday, November 29, 2010 9:44 AM
> > To: r-help at r-project.org
> > Subject: [R] how to use by() ?
> > 
> > Hello, All!
> > 
> > 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]
> >   }
> > }
> > 
> > 
> > Jim
> > 
> > 	[[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.
> > 
> 
> ______________________________________________
> 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.
> 



More information about the R-help mailing list