Bort Edwards
Bort Edwards

Reputation: 39

Loop names from two dataframe columns through an existing function in R

I have a list of plant names in a dataframe. Plant names come as a couplet with "genus" followed by "species". In my case the couplet is already split across columns (which should help). As a dummy example for three species (Helianthus annuus, Pinus radiatia, and Melaleuca leucadendra):

df <- data.frame(genus=c("Helianthus", "Pinus", "Melaleuca"), species=c("annuus","radiata", "leucadendra"))

I would like to use a function in the package "Taxize" to check these names against a database (IPNI). There is no batch function for this, and annoyingly the format for querying a single name is:

checked <- ipni_search(genus='Helianthus', species='annuus')

What I need is a loop to feed each genus name and it's associated species name into that function. I can do this for just genus:

list <- df$genus
checked <- lapply(list, function(z) ipni_search(genus=z))

but am tied up in all sorts of knots trying to pass the species with it.

Any help appreciated! Cheers

Upvotes: 1

Views: 34

Answers (1)

Gregor Thomas
Gregor Thomas

Reputation: 145860

Loop (or *apply) over the index, not the actual value:

checked = lapply(
  1:nrow(df),
  function(i) ipni_search(genus = df$genus[i], species = df$species[i])
)

Alternately, you can use Map which is made for iterating over multiple vectors/lists in parallel:

checked = Map(ipni_search, genus = df$genus, species = df$species)

Upvotes: 1

Related Questions