1
votes

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!

1

1 Answers

1
votes

How about this using plyr - PS if your remaining columns 6-10 are always the same for any combination of DATE/SITE, then this can be simplified further (no need for merge())

require(plyr)

sp<-read.csv("sp.csv")
sp<-sp[,2:10] #(take out the ID numbers from csv)

mins<-ddply(sp,.(YEAR,SITE,SP),summarise,DATE=min(DATE))
mins<-merge(sp,mins,by=c("YEAR","SITE","DATE"))
mins$DATE<-mins$DATE-7

maxs<-ddply(sp,.(YEAR,SITE,SP),summarise,DATE=max(DATE))
maxs<-merge(sp,maxs,by=c("YEAR","SITE","DATE"))
maxs$DATE<-maxs$DATE+7

sp.new<-rbind(mins,sp,maxs)
sp.new[order(sp.new$DATE),]

   YEAR            SITE  DATE     RN      NOM   LONG                                     SP SUMNB     NB100
1  2003 RNN027-Estagnol 12159 RNN027 ESTAGNOL 03 330 Anthocharis cardamines (Linnaeus,1758)     2 0.6060606
7  2003 RNN027-Estagnol 12166 RNN027 ESTAGNOL 03 330 Anthocharis cardamines (Linnaeus,1758)     2 0.6060606
12 2003 RNN027-Estagnol 12173 RNN027 ESTAGNOL 03 330 Anthocharis cardamines (Linnaeus,1758)     2 0.6060606
2  2005 RNN027-Estagnol 12888 RNN027 ESTAGNOL 01 540 Anthocharis cardamines (Linnaeus,1758)     2 0.3703704
10 2005 RNN027-Estagnol 12895 RNN027 ESTAGNOL 01 540 Anthocharis cardamines (Linnaeus,1758)     2 0.3703704
11 2005 RNN027-Estagnol 12910 RNN027 ESTAGNOL 01 540 Anthocharis cardamines (Linnaeus,1758)     2 0.3703704
13 2005 RNN027-Estagnol 12917 RNN027 ESTAGNOL 01 540 Anthocharis cardamines (Linnaeus,1758)     2 0.3703704
3  2006 RNN027-Estagnol 13245 RNN027 ESTAGNOL 03 330 Anthocharis cardamines (Linnaeus,1758)     2 0.6060606
8  2006 RNN027-Estagnol 13252 RNN027 ESTAGNOL 03 330 Anthocharis cardamines (Linnaeus,1758)     2 0.6060606
9  2006 RNN027-Estagnol 13257 RNN027 ESTAGNOL 03 330 Anthocharis cardamines (Linnaeus,1758)     2 0.6060606
14 2006 RNN027-Estagnol 13264 RNN027 ESTAGNOL 03 330 Anthocharis cardamines (Linnaeus,1758)     2 0.6060606
4  2011 RNN027-Estagnol 15049 RNN027 ESTAGNOL 02 310 Anthocharis cardamines (Linnaeus,1758)     1 0.3225806
5  2011 RNN027-Estagnol 15056 RNN027 ESTAGNOL 02 310 Anthocharis cardamines (Linnaeus,1758)     1 0.3225806
6  2011 RNN027-Estagnol 15075 RNN027 ESTAGNOL 02 310 Anthocharis cardamines (Linnaeus,1758)     1 0.3225806
15 2011 RNN027-Estagnol 15082 RNN027 ESTAGNOL 02 310 Anthocharis cardamines (Linnaeus,1758)     1 0.3225806