Skip to content

Instantly share code, notes, and snippets.

@prabhasp
Created March 13, 2013 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prabhasp/5155863 to your computer and use it in GitHub Desktop.
Save prabhasp/5155863 to your computer and use it in GitHub Desktop.
Massaging (a folder full of) NPC xls files into a manageable localities subset
setwd("~/Dropbox/FINAL CONPEDIUM OF LOCALITIES/")
require(gdata)
l <- list.files()
l <- l[which(str_detect(l, "\\.xls"))]
listofdfs <- llply(l, function(fname) {
print(paste("Reading file:", fname))
tryCatch({
d <- read.xls(fname)
feat_type_col <- names(d)[str_detect(names(d), "FEATURE.*TYPE")]
subset(d,
d[,feat_type_col]=='Locality', select=names(d)[which(str_detect(names(d), 'U.E'))])
}, warning = function(w) {
NA
}, error = function(e) {
print(paste("FAILED ON:", fname))
NA
})
})
# TO CHECK WHAT WAS a problem:
# ll[c(which(unlist(llply(lens, is.null))), 14)]
md <-
ldply(listofdfs, function(df) {
if (!is.data.frame(df)) data.frame() else
names(df)[which(str_detect(names(df), "L.NG"))] <- "LONG"
names(df)[which(str_detect(names(df), "LAT"))] <- "LAT"
names(df)[which(str_detect(names(df), "FEATURE.*NAME"))] <- "FEATURE_NAME"
names(df)[which(str_detect(names(df), "FEATURE.*TYPE"))] <- "FEATURE_TYPE"
df
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment