1

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.

Ariel C
  • 11
  • 2
  • 2
    Welcome to SO, ArielC! Please realize we know nothing about any of your datasets, so it seems unlikely we can give you something accurate and actionable. Please make this question *reproducible*. This includes sample code you've attempted (including listing non-base R packages, and any errors/warnings received), sample *unambiguous* data (e.g., `data.frame(x=...,y=...)` or the output from `dput(head(x))`), and intended output given that input. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Jan 02 '22 at 02:10
  • Thank you for the advise @r2evans ! Hopefully the question is a bit clearer now. – Ariel C Jan 02 '22 at 03:47
  • Your function doesn't return anything, try adding `return(Dhats)` at the end. – jpdugo17 Jan 02 '22 at 03:55
  • Thank you @jpdugo17 I've just added the return() function. – Ariel C Jan 02 '22 at 04:02
  • Is `lapply(as.character(c(1:4)), myfunc) ` what you were looking for? – jpdugo17 Jan 02 '22 at 04:08
  • Yes, I think that sounds like what I am looking for. But when I tried this (with the same function call as listed in the question), the error message still exist. – Ariel C Jan 02 '22 at 04:29
  • 1
    Your function isn’t using the argument, x, but instead a string, ‘x’. Remove the quotes to use the argument. If you need strings to index, and you loop over integers, you then also need to translate the numbers to strings. In either case, right now you are indexing on a hard coded string and not the function argument. – Thomas Mailund Jan 02 '22 at 04:37
  • Ok, thank you so much Thomas and @jpdugo17! The problem is solved now. – Ariel C Jan 02 '22 at 04:39

0 Answers0