I am new to R and I want to apply the same function to multiple data frames within a list. I want to calculate the overlapEst() for each of the individual zones in the data kerinci from the overlap package. I have a list which includes each zone as a separate data frame.
Here is the list
library(overlap)
dput(kerinci) #this is the data embedded in the overlap package
list.of.my.data <- split(kerinci, f = kerinci$Zone)
The function to do it once
timeRad <- kerinci$Time * 2*pi
spsA <- timeRad[kerinci$Zone == '1' & kerinci$Sps == 'tapir']
spsB <- timeRad[kerinci$Zone == '1' & kerinci$Sps == 'tiger']
Dhats <- overlapEst(spsA, spsB)
Dhats
The problem is, I need to repeat the same operation with zone 1, 2, 3, and 4 (so I need to change the zone name in the apostrophe), but I am not sure how to change the name in the loop function.
I saw that other posts suggest defining a function and using lapply(), but since in my case, the name of the data frame have to go inside the function, it doesn't seem to work (and I think it is probably because I didn't fully understand the loop function). The error message came out saying that I am "missing value where TRUE/FALSE is needed"
timeRad <- kerinci$Time * 2*pi
myfunc <- function(x) {
spsA <- timeRad[kerinci$Zone == x & kerinci$Sps == 'tapir']
spsB <- timeRad[kerinci$Zone == x & kerinci$Sps == 'tiger']
Dhats <- overlapEst(spsA, spsB)
return(Dhats)
}
lapply(list.of.my.data, myfunc)
Thank you so much for your help.