I'm pretty new to R, but it seems that this is a specific problem to which I have not been able to find an answer.
My program reads in some data, then rbinds certain columns of that data to one of several data frames based on a vector of column numbers I pass it, so something like this:
filename <- c("vector", "full", "of", "filenames")
colVal <- (32)
InMat <- data.frame()
for (i in 1:length(filename)){
file <- read.table(filename[i], header=TRUE, fill=TRUE, stringsAsFactors=FALSE)
InMat <- rbind(InMat, file[c(2:dim(file)[1], colVal)])
#...other matricies...
}
My issue lies in the case where there is only one desired column, i.e. colVal takes one value. In this case, I find that InMat is essentially transposed from what I would require. Worse, when I read in mulitple files, it rbinds the transposed desired column, so I get a number of rows equal to the number of files I'm reading, with as many columns as there are rows in each desired column of each file.
It seems that if there are 2 desired columns (i.e. colVal takes two or more values), then it acts as I expect (i.e. a column is read and stored in InMat as a column, columns from each additional file are stored below).
My question is why does rbind act differently when only one desired column value is passed to it, and if there is an easy way (read; not adding some clunky if or for loop to check) to avoid this?
Thanks!