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
--
Corey Sparks
Assistant Professor
Department of Demography and Organization Studies
University of Texas at San Antonio
One UTSA Circle
San Antonio, TX 78249
210-458-6858
corey.sparks at utsa.edu
https://rowdyspace.utsa.edu/users/ozd504/www/index.htm
Try this: split(mapply(function(x, y)pres.test(x, y), DF$pop1995, DF$pop2000), DF$country) On Wed, Oct 29, 2008 at 10:19 AM, Corey Sparks <corey.sparks@utsa.edu>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 > > -- > Corey Sparks > Assistant Professor > Department of Demography and Organization Studies > University of Texas at San Antonio > One UTSA Circle > San Antonio, TX 78249 > 210-458-6858 > corey.sparks@utsa.edu > https://rowdyspace.utsa.edu/users/ozd504/www/index.htm > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
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