[R] Setting elements in data frame

Peter Dalgaard BSA p.dalgaard at pubhealth.ku.dk
Thu Feb 22 16:30:38 CET 2001


Kaspar Pflugshaupt <pflugshaupt at cirsium.ethz.ch> writes:

>      `ifelse' returns a value with the same shape as `test' which is
>      filled with elements selected from either `yes' or `no' depending
>      on whether the element of `test' is `TRUE' or `FALSE'. If `yes' or
>      `no' are too short, their elements are recycled.
> 
> So could it be that because 6 (an integer) is present in the comparison, the 
> result _will_ be integer no matter what class z is? The help doesn't say so, 
> but I might overlook something basic. Still, checking this out:

Yes, it coerces and drops all attributes, including class. So factors
invariably become integers. For an extreme example, try

> ifelse(1:12==0,factor(month.name),factor(month.name))
 [1]  5  4  8  1  9  7  6  2 12 11 10  3


> > data.frame(lapply(x,function(x) ifelse(x==6.0,6000.0,x)))  
> # would give float results, if above were true
>   S1   N1   N2
> 1  2    2 6000
> 2  3    4   NA
> 3  4 6000    0
> 4  1    9 6000
> 
> still gives integer results. And the problem does not lie with the data.frame 
> conversion: 

You are using the wrong tool to check it. Numerics close enough to
integers print like that independently of storage mode. Have a look at
this:


> storage.mode(ifelse(1:12==1,0,factor(month.name)))
[1] "double"
> storage.mode(ifelse(1:12==1,as.integer(0),factor(month.name)))
[1] "integer"

(notice that the default numeric storage mode is double, 0.0 and 0 is
the same).

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list