Short answer: [.data.frame
(the [
operator on data frames) by default converts output to the lowest possible dimension (via the argument drop=TRUE
). If you're pulling just one column then it converts to a vector, which then creates a matrix with other vectors via rbind
into a matrix. When you extract two or more columns, you get a data frame, so the output of rbind
is a data frame.
The quick fix is to change this line:
InMat <- rbind(InMat, file[c(2:dim(file)[1], colVal)]) #old line
InMat <- rbind(InMat, file[c(2:dim(file)[1], colVal),drop=FALSE]) #new line
A more R-like way of coding this would be to use lapply
and call rbind
once. Because R is assign-by-copy, growing objects by repeated concatenating/adding is quite inefficient (see the second circle of the R Inferno).
filename <- c("vector", "full", "of", "filenames")
colVal <- (32)
dfm <- lapply(filename, read.table
, header=TRUE, fill=TRUE, stringsAsFactors=FALSE)
dfm <- lapply(dfm,`[`,colVal)
dfm <- do.call(rbind,dfm)
If you know the positions of the columns you want to extract beforehand, you could use the colClasses
argument of read.table
and skip over reading the entire table:
filename <- c("vector", "full", "of", "filenames")
colVal <- 32
cc <- rep.int("NULL",40) #where 40 is # of columns in table
cc[colVal] <- NA
dfm <- lapply(filename, read.table
, header=TRUE, fill=TRUE, colClasses=cc, stringsAsFactors=FALSE)
dfm <- do.call(rbind,dfm)