Home > OS >  Purrr package add species names to each output name in SSDM
Purrr package add species names to each output name in SSDM

Time:10-28

I have a list of species and I am running an ensemble SDM modelling function on the datset filtering by each species, to give an ensemble SDM per species from the dataset.

I have used purrr package to get it running, and the code works fine when there is no naming convention added in. However, when it outputs the Ensemble.SDM for each species, they are all named the same thing "ensemble.sdm", so when I want to stack them, I cannot as they are all named the same thing.

I would like to be able to name each output of the model something different, ideally linked to the species name picked out in the line: data <- Occ_full %>% filter(NAME == .x)

The working code is written below:

list_of_species <- unique(unlist(Occ_full$NAME))

# Return unique values

output <- purrr::map(limit_list_of_species, ~ {
  data <- Occ_full %>% filter(NAME == .x)
  ensemble_modelling(c('GAM'), data, Env_Vars,
            Xcol = 'LONGITUDE', Ycol = 'LATITUDE', rep = 1)
})

The code I have tried to get it named within it, is below, but it does not work, it names it with lots of repeitions of the row number.

output <- purrr::map(limit_list_of_species, ~ {
  data <- Occ_full %>% filter(NAME == .x)
  label <- as.character(data)
  ensemble_modelling(c('GAM'), data, Env_Vars,
            Xcol = 'LONGITUDE', Ycol = 'LATITUDE', rep = 1,  name = label )
})

Could anyone help me please? I simply want each "output" to be named with the species name specified in the filter. Thank you

CodePudding user response:

Try using split with imap -

list_of_species <- split(Occ_full, Occ_full$NAME)

output <- purrr::imap(list_of_species,~{
  ensemble_modelling(c('GAM'), .x, Env_Vars,Xcol = 'LONGITUDE', 
                     Ycol = 'LATITUDE', rep = 1, name = .y)
})

split would ensure that the list_of_species is named which can be used in imap.

  • Related