[R] with(data.frame,ifelse(___,___,___))

William Dunlap wdunlap at tibco.com
Thu Dec 23 02:42:38 CET 2010


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Jim Moon
> Sent: Wednesday, December 22, 2010 5:05 PM
> To: R-help at r-project.org
> Subject: [R] with(data.frame,ifelse(___,___,___))
> 
> Hello, All,
> 
> Mac OS 10.6.5
> R64 2.11.1
> 
> This works as expected:
> 
> f1 = c(0.084, 0.099, 0)
> data= data.frame(f1)
> data$f1=with(data,ifelse(f1==0, 0.0001, f1))
> data
> 
>      f1
> 1 0.0840
> 2 0.0990
> 3 0.0001
> 
> Substituting 'f1==0' with 'T' produces the expected result:
> 
> f1 = c(0.084, 0.099, 0)
> data= data.frame(f1)
> data$f1=with(data,ifelse(T, 0.0001, f1))

It produces those results accidently.  Look
at the output of the ifelse call, before you
stuff it into data$f1:
  > ifelse(TRUE, 0.0001, data$f1)
  [1] 1e-04
The output of ifelse(condition,ifTrue,ifFalse)
is the length of the condition argument and has
nothing to do with the length of the ifTrue
or ifFalse arguments.  `$<-.data.frame`(x,name,value)
will expand the value argument to the nrow(x).

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> data
> 
>      f1
> 1 1e-04
> 2 1e-04
> 3 1e-04
> 
> But, substituting 'f1==0'  or 'T' with 'F' does not produce 
> the expected result:
> 
> f1 = c(0.084, 0.099, 0)
> data= data.frame(f1)
> data$f1=with(data,ifelse(F, 0.0001, f1))
> data
> 
>      f1
> 1 0.084
> 2 0.084
> 3 0.084
> 
> I would expect this:
>      f1
> 1 0.084
> 2 0.099
> 3 0
> 
> Is with(data.frame,ifelse(___,___,___)) broken, or does my 
> interpretation of it need fixing?
> 
> Thank you for your time.
> 
> 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.
> 



More information about the R-help mailing list