[R] Help using tapply with multiple variables

Corey Sparks corey.sparks at UTSA.EDU
Wed Oct 29 13:19:57 CET 2008

Dear list,
I have the function (as a simple example, which is actually part of a  
larger function)

pres.test<-function(N0=N0, N1=N1)

which calculates the annual growth rates in a population

Where N0 is the population classified into age intervals, say 5  
years, at time=1995, and N1 is the population by 5 year age classes  
at time=2000.

For example some data like this are:
country   pop1995 pop2000
1     1712    1940
1     1329    1535
1     1101    1268
1     911    1048
1      758     859
2      627     710
2      513     584
2      420     475
2     754     965
2     638     741

I want to use the tapply function to apply the pres.test function  
over all countries in my data table (the real data have ~150  
countries and 10 age classes), but I can't seem to get tapply to take  
as input to FUN more than one variable, for example I tried:

popdat<-cbind(dat2.sub$pop1995, dat2.sub$pop2000) #try to force the  
two time points into a vector form

tapply(popdat, dat2.sub$country, pres.test, ...=list(N0=popdat[,1],  
N1=popdat[,2])) #tried to pass the other arguments that pres.test  
needs  via ...

And got the error:
Error in tapply(popdat, dat2.sub$Country, pres.test, ... = list(N0 =  
popdat[,  :
   arguments must have same length

I see that the function mapply will take multiple arguements, but I  
don't think it will use an INDEX like tapply.

Any comments or clarification would be most appreciated.


Corey Sparks
Assistant Professor
Department of Demography and Organization Studies
University of Texas at San Antonio
One UTSA Circle
San Antonio, TX 78249
corey.sparks at utsa.edu

More information about the R-help mailing list