From p.dalgaard at biostat.ku.dk  Thu Nov  5 00:28:51 2009
From: p.dalgaard at biostat.ku.dk (Peter Dalgaard)
Date: Thu, 05 Nov 2009 00:28:51 +0100
Subject: [Rd] sapply improvements
In-Reply-To: <77EB52C6DD32BA4D87471DCD70C8D700021320EE@NA-PA-VBE03.na.tibco.com>
References: <20091104160510.1CF24282BE82@mail.pubhealth.ku.dk>	<4AF1B00D.7060809@stats.uwo.ca>	<77EB52C6DD32BA4D87471DCD70C8D70002131F9B@NA-PA-VBE03.na.tibco.com>	<4AF1E2EC.7060405@stats.uwo.ca>
	<77EB52C6DD32BA4D87471DCD70C8D700021320C6@NA-PA-VBE03.na.tibco.com>
	<4AF1EF13.3090900@biostat.ku.dk>
	<77EB52C6DD32BA4D87471DCD70C8D700021320EE@NA-PA-VBE03.na.tibco.com>
Message-ID: <4AF20E33.7070104@biostat.ku.dk>

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



[Rd] sapply improvements
Peter Dalgaard