[R] Help using tapply with multiple variables
Dimitris Rizopoulos
d.rizopoulos at erasmusmc.nl
Wed Oct 29 13:31:26 CET 2008
try this:
lapply(split(dat, dat$country), function (x) {
pres.test(x$pop1995, x$pop2000)
})
I hope it helps.
Best,
Dimitris
Corey Sparks wrote:
> 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)
> {
> dt<-5
> r<-log(N1/N0)/dt
> r
> }
>
> 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
>
--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus Medical Center
Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
More information about the R-help
mailing list