Hi, I have the following function:

getDataFromDVFileCustom <- function (file, hasHeader = TRUE, separator =
"\t") 
{
DVdatatmp <- as.matrix(read.table(file, sep = "\t", fill = TRUE,
comment.char = "#", as.is = TRUE, stringsAsFactors = FALSE, na.strings =
"NA"))
DVdatatmper <- as.matrix(DVdatatmp[ , c("datetime",
grep("^_00060_00003", colnames(DVdatatmp)))])
retval <- as.data.frame(DVdatatmper, colClasses = c("character"), fill =
TRUE, comment.char = "#", stringsAsFactors = FALSE)
if (ncol(retval) == 2) {
names(retval) <- c("dateTime", "value")
}
else if (ncol(retval) == 3) {
names(retval) <- c("dateTime", "value", "code")
}
if (dateFormatCheck(retval$dateTime)) {
retval$dateTime <- as.Date(retval$dateTime)
}
else {
retval$dateTime <- as.Date(retval$dateTime, format = "%m/%d/%Y")
}
retval$value <- as.numeric(retval$value)
return(retval)
}

The function gives me this error:
getDataFromDVFileCustom(file)
Error in as.matrix(DVdatatmp[, c("datetime", grep("^_00060_00003",
colnames(DVdatatmp)))]) : 
subscript out of bounds

I am trying to only select 3 columns (datetime and then two partial name
columns that end in 00060_00003 and 00060_00003_cd. Each file that I
will be reading into the function has a different number of columns and
a different prefix in front of 00060_00003 and 00060_00003_cd. I have
searched online and tried those possible solutions, but they did not
work for my function and data.

What is the best way to select those 3 columns only?

Thank-you.

Irucka Embry 


<span id=m2wTl><p><font face="Arial, Helvetica, sans-serif" size="2" 
style="font-size:13.5px">_______________________________________________________________<BR>Get
 the Free email that has everyone talking at <a href=http://www.mail2world.com 
target=new>http://www.mail2world.com</a><br>  <font color=#999999>Unlimited 
Email Storage &#150; POP3 &#150; Calendar &#150; SMS &#150; Translator &#150; 
Much More!</font></font></span>
        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to