简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM