[R] ave returns wrong type

OKB (not okblacke) brenbarn at brenbarn.net
Wed Apr 15 09:09:42 CEST 2009


    	I've been using the ave function to compute some statistics on a 
data frame.  After a while I noticed that, for some reason, it was 
returning numerical statistics as strings instead of numbers.  I delved 
into the code of the functions and traced the problem to the following 
fact:

    	ave uses split<- to do its work.  Specifically, it does "split(x, 
g) <- lapply(split(x, g), FUN)".  The problem is that this assigns the 
result of FUN into the original vector x, thus acquiring the mode of 
that vector.  So if you do ave(x, g, f) to apply f to x as grouped by g, 
and the result has the type of x, not the type that f returns.  So you 
get what strikes me as very annoying behavior, viz.:

>ave(rep(c("X", "Y"), 15), rep(c("A", "B", "C"), times=10), FUN=length)
"10" "10" "10" "10" "10" "10" "10" "10" "10" "10" "10" "10" "10" "10" 
"10" "10" "10" "10" "10" "10" "10" "10" "10" "10"
[25] "10" "10" "10" "10" "10" "10"

    	"length" returns numbers, so I want the result vector to contain 
numbers, obviously.  I can of course work around this by explicitly 
converting the result vector to the data type I want, but it seems silly 
for ave to do this.  ave applies a function to some stuff; the result 
should clearly depend on the RETURN TYPE of the function (as, for 
instance, tapply does it), not the type of the data being summarized by 
that function.

    	Is this just a bug?  Is there any known way to deal with this other 
than just manually casting the data to the type I need?

Thanks,
-- 
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead.  Go, instead, where there is
no path, and leave a trail."
	--author unknown



More information about the R-help mailing list