Re: [Rd] sapply improvements

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Thu, 05 Nov 2009 00:28:51 +0100

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

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Wed 04 Nov 2009 - 23:32:07 GMT

This archive was generated by hypermail 2.2.0 : Thu 05 Nov 2009 - 11:30:32 GMT