Skip to content

Instantly share code, notes, and snippets.

@PhillRob
Last active September 1, 2015 11:44
Show Gist options
  • Save PhillRob/6c3f41e2e925102dfa05 to your computer and use it in GitHub Desktop.
Save PhillRob/6c3f41e2e925102dfa05 to your computer and use it in GitHub Desktop.
this runs through nzpcn.org.nz and gets native range and naturalised year. used to run: w<-matrix(rep(1:7700)) test <- sapply(w, function(x) nat.year.nzpcn(x))
nat.year.nzpcn <- function(x) {
sp <- getURL(paste0("http://nzpcn.org.nz/flora_details.aspx?ID=", x))
if (length(grep("Error: Cannot find species with ID =", sp))) {
return("Not_found")
} else {
flush.console()
sp.st <- unlist(gregexpr(pattern = "<title>", sp, ignore.case = F,
fixed = T))
sp.end <- unlist(gregexpr(pattern = " | New Zealand Plant Conservation Network",
sp, ignore.case = F, fixed = T))
species <- unique(substr(sp, sp.st + 9, sp.end))
nat <- unlist(gregexpr(pattern = "Year Naturalised", sp, ignore.case = F,
fixed = T))
org <- unlist(gregexpr(pattern = "Origin", sp, ignore.case = F,
fixed = T))
if ((min(nat) <= 0) && (min(org) <= 0)) {
results <- c(species, "NA", "NA")
}
if ((min(nat) >= 0) && (min(org) <= 0)) {
nat.year <- unique(substr(sp, nat + 24, nat + 27))
results <- c(species, nat.year, "NA")
}
if ((min(nat) <= 0) && (min(org) >= 0)) {
org.loc <- unique(substr(sp, org + 14, org + 47))
org.loc <- sub("</p>.*", "", org.loc)
results <- c(species, "NA", org.loc)
} else {
nat.year <- unique(substr(sp, nat + 24, nat + 27))
org.loc <- unique(substr(sp, org + 14, org + 47))
org.loc <- sub("</p>.*", "", org.loc)
results <- c(species, nat.year, org.loc)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment