[Rd] sapply improvements

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Nov 5 00:28:51 CET 2009


William Dunlap wrote:
...
>>
>> if (x <= 0) NA else log(x)
>>
>> variety otherwise.
> 
> Would you only want it to coerce upwards to FUN.VALUES's
> type?  E.g., allow
>    sapply(z, length, FUN.VALUE=numeric(1))
> to return a numeric vector but die on
>    sapply(z, function(zi)as.complex(zi[1]), FUN.VALUE=numeric(1))
> If the latter doesn't die should it return
> a complex or a numeric vector?  (I'd say it
> needs to be numeric, but I'd prefer that it
> died.)

I'd say that it should probably die on downwards coercion. Getting a 
double when an integer is expected, or complex instead of double as you 
indicate, is a likely user error. If not, then the user can always 
coerce explicitly inside FUN.

Another issue is whether one would want to go beyond the base classes of 
  S (logical, integer, double, complex, character). For other classes, 
there may be no notion of "up" and "down" in coercion. Then again, 
sapply was always limited to what unlist() will handle, so e.g.

 > sapply(1:10,FUN=function(i)Sys.Date())
  [1] 14553 14553 14553 14553 14553 14553 14553 14553 14553 14553

as opposed to

 > structure(rep(14553,10), class="Date")
  [1] "2009-11-05" "2009-11-05" "2009-11-05" "2009-11-05" "2009-11-05"
  [6] "2009-11-05" "2009-11-05" "2009-11-05" "2009-11-05" "2009-11-05"




-- 
    O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907



More information about the R-devel mailing list