Skip to content

Instantly share code, notes, and snippets.

Created March 18, 2009 02:53
Show Gist options
  • Save anonymous/80907 to your computer and use it in GitHub Desktop.
Save anonymous/80907 to your computer and use it in GitHub Desktop.
scanData.go <- function(inFileName, colName, colType, headerFg = FALSE, sep = ",", ...){
if(length(colName)>length(colType[!colType == "NULL"])) stop("more column names than columns")
skip <- 0
if(headerFg){
skip <- 1
}
tmp1 <- vector("list",length(colType))
notNull <- which(!colType == "NULL", arr.ind = T)
for(i in 1:length(notNull)){
tmp <- colType[notNull[i]]
switch(match(tmp,c("char","num","logical")),
tmp1[[notNull[i]]] <- "character",
tmp1[[notNull[i]]] <- double(0),
tmp1[[notNull[i]]] <- logical)
}
out <- scan(inFileName, what = tmp1, skip = skip, sep = sep)
out <- out[!(colType == "NULL")]
if(length(colName) < length(out)){
colName <- c(colName, paste("V", 1:(length(out)-length(colName)), sep = ""))
warning("header and 'col.names' are of different lengths")
}
names(out) <- colName
return(out)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment