0

I have function that takes the form

myfun<-function(Year, ID, Species, Abundance, resamps) {do something}

"resamps" means resampling and it takes a single number (e.g. 20)

I had no problem applying the function over a dataframe as it accepts data in the form:

<-myfun(mydata$Year, mydata$Bay, mydata$Species, mydata$Abundance, 20)

I need to apply this function over a list with the following structure

 $ TX_ARAB:Classes ‘tbl_df’, ‘tbl’ and 'data.frame':    308670 obs. of  4 variables:
  ..$ Year     : chr [1:308670] "1982" "1982" "1982" "1982" ...
  ..$ Bay      : chr [1:308670] "TX_ARAB" "TX_ARAB" "TX_ARAB" "TX_ARAB" ...
  ..$ Species  : chr [1:308670] "Anchoa mitchilli" "Brevoortia patronus" "Dorosoma cepedianum" "Leiostomus xanthurus" ...
  ..$ Abundance: num [1:308670] 1 243 1 24 3 6 26 6 1 1 ...
 $ TX_COCB:Classes ‘tbl_df’, ‘tbl’ and 'data.frame':    344467 obs. of  4 variables:
  ..$ Year     : chr [1:344467] "1982" "1982" "1982" "1982" ...
  ..$ Bay      : chr [1:344467] "TX_COCB" "TX_COCB" "TX_COCB" "TX_COCB" ...
  ..$ Species  : chr [1:344467] "Anchoa mitchilli" "Micropogonias undulatus" "Ariopsis felis" "Archosargus probatocephalus" ...
  ..$ Abundance: num [1:344467] 6 17 10 1 16 3 2 42 1 11 ...

It's no surprise that it didn't work when I did

d = lapply(mylist, myfun, resamps = 20)

I don't know how to get the function to accordingly use relevant object in the list given that I had to pass the data name with the objects when it was applied over a dataframe.

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
  • 5
    `d <- lapply(myList, function(x) myfun(x$Year, x$Bay, x$Species, x$Abundance, 20))` or you change `myfun` in order to take only a `data.frame` and `resamps` as arguments, then `d <- lapply(myList, myfunMod, resamps = 20)`. – davide May 07 '19 at 15:50
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Share data in *reproducible* format and provide functions that actually do something for testing purposes. – MrFlick May 07 '19 at 16:00
  • @davide this worked perfectly. Thanks. – user11384727 May 07 '19 at 16:26

0 Answers0