[R] length error using tapply()

Ben Bolker bolker at ufl.edu
Sat Oct 17 05:05:46 CEST 2009




George Aldridge wrote:
> 
> I am new to R and trying to aggregate a character variable. I have
> searched, and tapply() seems to hold the best hope, but I am having
> trouble with it. I have a character variable 'hab' and 3 factors all of
> the same length:
> 
>> length(hab)
> [1] 105219
>> length(sp)
> [1] 105219
>> length(pl)
> [1] 105219
>> length(yr)
> [1] 105219
> 
> However, when I try to aggregate the character variable by the 3 factors,
> I get an error:
> 
>> habs <- tapply(hab, c(sp,pl,yr))
> Error in tapply(hab, c(sp, pl, yr)) : arguments must have same length
> 
> Is there any way to aggregate a character variable?
> 
> 

Not sure what you want to do.  Can you show us with a small made-up example,
or a small subset of your data?  Normally the format of tapply() is
tapply(variable_to_aggregate,list(factor_1,factor_2,factor_3,...),aggregating_function)
[list() rather than c() is definitely the correct way to specify the list of
factors
by which to aggregate!]
Do you just want to tabulate the habitat types by the other three factors?
table(hab,sp,pl,yr) ...

-- 
View this message in context: http://www.nabble.com/length-error-using-tapply%28%29-tp25917650p25935019.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list