I'm am currently trying to compute a species abundance index and i'm a bit stuck with the do.call command.
I have a DF built like this:
YEAR RN DATE NOM SITE LONG SP SUMNB NB100
1 2011 RNN027 15056 ESTAGNOL RNN027-Estagnol 02 310 Anthocharis cardamines (Linnaeus, 1758) 1 0.3225806
2 2011 RNN027 15075 ESTAGNOL RNN027-Estagnol 02 310 Anthocharis cardamines (Linnaeus, 1758) 1 0.3225806
3 2003 RNN027 12166 ESTAGNOL RNN027-Estagnol 03 330 Anthocharis cardamines (Linnaeus, 1758) 2 0.6060606
4 2006 RNN027 13252 ESTAGNOL RNN027-Estagnol 03 330 Anthocharis cardamines (Linnaeus, 1758) 2 0.6060606
5 2006 RNN027 13257 ESTAGNOL RNN027-Estagnol 03 330 Anthocharis cardamines (Linnaeus, 1758) 2 0.6060606
6 2005 RNN027 12895 ESTAGNOL RNN027-Estagnol 01 540 Anthocharis cardamines (Linnaeus, 1758) 2 0.3703704
7 2005 RNN027 12910 ESTAGNOL RNN027-Estagnol 01 540 Anthocharis cardamines (Linnaeus, 1758) 2 0.3703704
In order to calculate my index, i have to isolate every SITE/YEAR combination and take the first and last date to edit them (minus and plus 7 days).
I should be able to to do that with the following command (not working because to finished):
do.call(rbind, by(DF, DF[c("YEAR","SITE")], FUN = function(x) {
tmp <- x[c(1, nrow(x)), ]
tmpmin<-min(tmp$DATE)
tmpmax<-max(tmp$DATE)
tmp1<-tmp1-7
tmp2<-tmp2+7
return(tmp)
But I dont know how to complete my command to fit to what i want: I need to keep the modified dates, and add them respectivly before and after every combination of SITE/YEAR/SP. The point is to detect the first and last observation date per site for the sum of all the species, modify them as i want, and add them in the temporal repartition of each species i got (two new rows in each "chunk").
I'm am able to add a row before and after every "chunk" with the SP criteria with the following code (but the row is for now based and the first and last dates, not the dates i want):
do.call(rbind, by(DF, DF[c("YEAR","SITE", "SP")], FUN = function(x) {
tmp <- x[c(1, seq(nrow(x)), nrow(x)), ]
tmp$DATE[1] <- tmp$DATE[1] - 7
tmp$DATE[nrow(tmp)] <- tmp$DATE[nrow(tmp)] + 7
return(tmp)
}))
My question is, how can I link these two commands to succesfully add the rows containing the dates of my first command (SITE/YEAR) to the second command (SITE/YEAR/SP). I tried to add a loop in my function(x) command, as well as another do.call command, but it wasn't working.
EDIT:
@Troy: Yesterday, i succesfully add a loop in my do.call command: my goal was to subset every SITE/YEAR combination, regardless of the species. In each subset, i take the two limits of my temporal distribution for the sum of species(because i dont have that info).Then i write a row for each species with the infos i gathered in the subsets. My loop here write me a new dataframe with N rows, for N species with the min and max dates(see below). I will further merge this dummy dataframe with my actual DF.
MIN<-data.frame(matrix(NA, nrow = 100, ncol = 9))
colnames(MIN)<-c("YEAR","RN","DATE","NOM","SITE","LONG","SP","SUMNB","NB100" )
MAX<-data.frame(matrix(NA, nrow = 100, ncol = 9))
colnames(MAX)<-c("YEAR","RN","DATE","NOM","SITE","LONG","SP","SUMNB","NB100" )
head(do.call(rbind, by(AGG100, AGG100[c("YEAR","SITE")], FUN = function(x) {
splist<-unique(x$SP)
lsp<-length(splist)
for (i in 1:lsp){
MIN$SP[i]<-as.character(splist[i])
MIN$SITE[i]<-as.character(unique(x$SITE))
MIN$DATE[i]<-as.character(min(x$DATE) - 7)
MIN$RN[i]<-as.character(unique(x$RN))
MIN$YEAR[i]<-as.character(unique(x$YEAR))
MIN$NOM[i]<-as.character(unique(x$NOM))
MIN$LONG[i]<-as.numeric(unique(x$LONG))
MIN$SUMNB[i]<-0
MIN$NB100[i]<-0
MAX$SP[i]<-as.character(splist[i])
MAX$SITE[i]<-as.character(unique(x$SITE))
MAX$DATE[i]<-as.character(min(x$DATE) + 7)
MAX$RN[i]<-as.character(unique(x$RN))
MAX$YEAR[i]<-as.character(unique(x$YEAR))
MAX$NOM[i]<-as.character(unique(x$NOM))
MAX$LONG[i]<-as.numeric(unique(x$LONG))
MAX$SUMNB[i]<-0
MAX$NB100[i]<-0
MINMAX<- rbind(MIN,MAX)
MINMAX<-MINMAX[complete.cases(MINMAX),]
}
return(MINMAX)
})), n=50)
YEAR RN DATE NOM SITE LONG SP
1 2003 RNN027 2003-04-10 ESTAGNOL RNN027-Estagnol 01 540 Brintesia circe (Fabricius, 1775)
2 2003 RNN027 2003-04-10 ESTAGNOL RNN027-Estagnol 01 540 Carcharodus alceae (Esper, 1780)
3 2003 RNN027 2003-04-10 ESTAGNOL RNN027-Estagnol 01 540 Celastrina argiolus (Linnaeus, 1758)
4 2003 RNN027 2003-04-10 ESTAGNOL RNN027-Estagnol 01 540 Coenonympha dorus (Esper, 1782)
5 2003 RNN027 2003-04-10 ESTAGNOL RNN027-Estagnol 01 540 Coenonympha pamphilus (Linnaeus, 1758)
EDIT 2: It is now working, thanks for the help!