2

I have a dataset with ~10,000 species. For each species in the dataset I want to query the IUCN database for threats facing each species. I can do this with one species at a time using the rl_threats function from the package rredlist. Below is an example of the function, this example pulls the threats facing Fratercula arctica and assigns them to the object test1 (key is a string that serves as a password for using the IUCN API that stays constant, parse should be TRUE but not as important).

    test1<-rl_threats(name="Fratercula arctica", 
                  key = '1234',
                  parse = TRUE)

I want to get threats for all 10,000 species in my dataset. My idea is to use a loop that passes in the names from my dataset into the name=" " field in the rl_threats command. This is a basic loop I tried to construct to do this but I'm getting lots of errors:

   for (i in 1:df$scientific_name) {
    rl_threats(name=i, 
             key = '1234',
             parse = TRUE)
    } 

How would I pass the species names from the scientific_name column into the rl_threats function such that R would loop through and pull threats for every species?

Thank you.

1 Answer 1

1

You can create a list to store the output.

result <- vector('list', length(df$scientific_name))
for (i in df$scientific_name) {
  result[[i]] <- rl_threats(name=i, key = '1234', parse = TRUE)
}

You can also use lapply :

result <- lapply(df$scientific_name, function(x) rl_threats(name=x, key = '1234', parse = TRUE))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.