[R] different functions on different vector subsets

Berton Gunter gunter.berton at gene.com
Thu Nov 10 18:04:53 CET 2005


Oops. Sorry. Should be:

sign(a)*2^a

where I assume you meant the inverse value should be -1/2^|a| = - 2^a for
a<0

-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA
 
"The business of the statistician is to catalyze the scientific learning
process."  - George E. P. Box
 
 

> -----Original Message-----
> From: bgunter 
> Sent: Thursday, November 10, 2005 9:00 AM
> To: Ron Ophir; r-help at stat.math.ethz.ch
> Subject: RE: [R] different functions on different vector subsets
> 
> The error messages mean what they say.
> 
> > I am trying to apply two different functions on on a vector 
> as follow:
> > a<-c(NA,1,2,3,-3,-4,-6)
> > if a>0 I would like to raise it by the power of 2: 2^a and if 
> > the a<0 I
> > would like to have the inverse value, i.e., -1/2^a.
> ## I assume you mean 1/(2^a). If not, modify the following 
> appropriately.
> 
> 2^(a*sign(a))  ## will do
> 
> As for your error message for:
> > a[a<0]<-(-1)/2^a[a<0]
> 
> a<0 has an NA at the first index and so R doesn't know what 
> index you want to assign the value to. Ergo the error message.
> 
> -- Bert Gunter
> Genentech Non-Clinical Statistics
> South San Francisco, CA
>  
> "The business of the statistician is to catalyze the 
> scientific learning process."  - George E. P. Box
>  
>  
> 
> > -----Original Message-----
> > From: r-help-bounces at stat.math.ethz.ch 
> > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ron Ophir
> > Sent: Thursday, November 10, 2005 7:26 AM
> > To: r-help at stat.math.ethz.ch
> > Subject: [R] different functions on different vector subsets
> > 
> > Hi,
> > I am trying to apply two different functions on on a vector 
> as follow:
> > a<-c(NA,1,2,3,-3,-4,-6)
> > if a>0 I would like to raise it by the power of 2: 2^a and if 
> > the a<0 I
> > would like to have the inverse value, i.e., -1/2^a.
> > so I thought of doing it two steps:
> > a[a>0]<-2^[a>0]
> > a[a<0]<-(-1)/2^a[a<0]
> > I got the following error
> > Error: NAs are not allowed in subscripted assignments
> > any other ma>nupulation that I did with is.na() but did not succeed.
> > What is funny that the two sides of the assignment work and 
> return the
> > same vector size:
> > > 2^a[a>0]
> > [1] NA  2  4  8
> > > a[a>0]
> > [1] NA  1  2  3
> > 
> > I found a solution in term of:
> > sapply(a,function(x) if (is(s.na)) NA else if (x<0) 
> (-1)/2^x else 2^x)
> > but still I would like to understand why the solution above did not
> > work. I think is more ellegant.
> > my R version is:
> > > sessionInfo()
> > R version 2.2.0, 2005-10-06, i386-pc-mingw32 
> >  
> > attached base packages:
> > [1] "methods"   "stats"     "graphics"  "grDevices" "utils"    
> > "datasets" 
> > [7] "base" 
> > Thanks,
> > Ron
> >  
> > 
> > 	[[alternative HTML version deleted]]
> > 
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! 
> > http://www.R-project.org/posting-guide.html
> > 
>




More information about the R-help mailing list