If you are only extracting and 'gluing' together a single column of values from each file, I would suggest using the concatenation function, c()
and creating a vector
, instead of creating a list
type object. Something along the lines of this should work:
fnames <-c("fname1","fname2","fname3")
excol="extractedColumnName"
extractedData = c() #initialize the vector. Typing can be determined by R automatically.
for(fname in fnames){
cur <- read.csv(fname, header=T)
extractedData = c(extractedData, cur[,excol])
}
Depending on how NAs are stored in your data files, an na.strings = "<the string used to indicate NA>"
argument may be necessary for the call to read.csv
. If there are character values in the columns you want, you may need to run the as.numeric()
function on the vector after all is read in.
There are more efficient, more coding-intensive way to load the data, but for a simple solution, not dealing with too large of data files, this method should work fine.
PS, To deal with the NAs (assuming you do not want to treat them in any special way) one of these two approaches should work:
1):
extractedDataNoNA = extractedData[ ! is.na(extractedData) ]
meanResult = mean(extractedDataNoNA)
The ! is.na(extractedData)
creates a logical vector to select elements in the extractedData vector.
2):
meanResult = mean(extractedData, na.rm=TRUE)