Created
March 13, 2013 20:32
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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