Skip to content

Instantly share code, notes, and snippets.

@sckott
Created April 7, 2014 22:36
Show Gist options
  • Save sckott/10069581 to your computer and use it in GitHub Desktop.
Save sckott/10069581 to your computer and use it in GitHub Desktop.

load taxize

library("taxize")

read in data, change to your path

dat <- read.csv("~/Downloads/TPWD Taxonomy Subset.csv")

search for tsns, then get classification data

res <- get_tsn(dat$scientificName_clean, searchtype = "sciname", ask = TRUE, verbose = TRUE)
out <- classification(res, db = "itis")

for each output in the list, make the name column a vector, then assign names of each vector element with the ranks, then make a data.frame. The function ldply take a list and iterates over it applying the function defined in the ldply call, then combines output to a data.frame.

library("plyr")
dat2 <- ldply(out, function(x){
  ss <- x$name
  names(ss) <- x$rank
  data.frame(t(ss), stringsAsFactors = FALSE)
})
names(dat2)[1] <- "tsn"
dat2[1:6,1:4]
     tsn   Kingdom Subkingdom  Infrakingdom
1 645263  Animalia  Bilateria Deuterostomia
2 155469  Animalia  Bilateria   Protostomia
3 156862  Animalia  Bilateria Deuterostomia
4  11391 Chromista  Chromista          <NA>
5 157325  Animalia  Bilateria Deuterostomia
6  78156  Animalia  Bilateria   Protostomia
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment