[R] Length as fun.aggregate in cast function of reshape package: unexpected error

David Wyllie David.Wyllie at ndm.ox.ac.uk
Fri Mar 12 11:33:01 CET 2010

Dear Everyone,

I am having problems with use of the reshape package's cast function using length as an aggregating function.
Unexpectedly,  I receive the error: 2 arguments passed to 'length' which requires 1
I don't understand this at all - the data I'm using is very simple, and appears almost identical to that used in the 
ChickWeight example in the package.  The problem can be reproduced and is described in more detail below, along with the rationale for what we're trying to do.  

Any help would be very gratefully received.
Best wishes
David Wyllie

(code starts here)
#  when using the reshape package to summarise laboratory data,
# an unexpected error message occurs then using this fun.aggregate= length.
# tested on R2.9.1 and R2.10.0 on XP and Vista 

# the error message is :
# Error in FUN(X[[1L]], ...) : 
# 2 arguments passed to 'length' which requires 1

# other summary functions, including mean, range, sd work as expected.
# I am new at both R and reshape, so there maybe something very basic that
# I don't understand here.

# in the real situation a data table is pulled using rodbc from an sql server.
# the sql server holds the data in a 'long' format - an example is below -
# which appears (to me) to be identical to that generated by melt.
# no melt operation is therefore carried out on the data, which looks exactly like the
# 'testdata' dataframe described below.

# testdata contains four numeric biochemistry results (Na,K,Ur,Cr) from a single test, 
# identified by having rowid=1.
# the objective is to compute summaries of this table by a variety of 
# explanatory variables; in this case, there is only one, 'rowid'.
# I had thought that reshape would be an easy and consistent way of doing this,
# and so it is, for means, max, min and other standard statistics, 
# but I can't work out how to count the number of observations in each cell
# of the resulting data frame.  I had thought that length would do this, but it doesn't.

# load the reshape package

# define a test data set
testdata <- data.frame(
   rowid = (c(1,1,1,1)),
   variable = c("Na", "K", "Ur", "Cr"),
   value = c(130, 4, 5,100)

# show it

# defining rowid as a factor doesn't alter the problem below - am unclear whether this is needed at all by reshape

# works correctly
cast(testdata, variable~., mean, na.rm=TRUE)

# works correctly
cast(testdata, ~., mean, na.rm=TRUE)

# works correctly
cast(testdata, ~.,fun.aggregate=mean, na.rm=TRUE)

# min, max, sd, range also work as expected

# all the below fail with "2 arguments passed to 'length' which requires 1"
cast(testdata, variable~., fun.aggregate=length, na.rm=TRUE)
cast(testdata, variable~., length, na.rm=TRUE)
cast(testdata, ~., length, na.rm=TRUE)

More information about the R-help mailing list