Skip to content
Prev 299639 / 398503 Next

Extracting arithmetic mean for specific values from multiple .txt-files

Hello,

Ok, I think that there were two problems.
One, gsub substitutes all (g - global) occurrences of the search 
pattern, so both periods were removed.
The other, it would allways consider column 8 as character, but when 
there are no values with two periods it's read in with class numeric.
Both are now corrected.



fun <- function(x, skip = 14){
     dat <- read.table(x, skip=skip, stringsAsFactors = FALSE)
     if(is.character(dat[, 8])){
         len <- sapply(strsplit(dat[, 8], "\\."), length)
         dat[len == 3 , 8] <- sub("\\.", "", dat[len == 3 , 8])
         dat[, 8] <- as.numeric(dat[, 8])
     }
     mean(dat[, 8])
}

sapply(list.files(pattern="XYZ.*\\.txt"), fun)


Rui Barradas

Em 10-07-2012 09:35, vimmster escreveu:

Thread (16 messages)

vimmster Extracting arithmetic mean for specific values from multiple .txt-files Jul 8 jim holtman Extracting arithmetic mean for specific values from multiple .txt-files Jul 8 vimmster Extracting arithmetic mean for specific values from multiple .txt-files Jul 9 Rui Barradas Extracting arithmetic mean for specific values from multiple .txt-files Jul 9 vimmster Extracting arithmetic mean for specific values from multiple .txt-files Jul 9 jim holtman Extracting arithmetic mean for specific values from multiple .txt-files Jul 9 Rui Barradas Extracting arithmetic mean for specific values from multiple .txt-files Jul 9 vimmster Extracting arithmetic mean for specific values from multiple .txt-files Jul 10 Rui Barradas Extracting arithmetic mean for specific values from multiple .txt-files Jul 10 vimmster Extracting arithmetic mean for specific values from multiple .txt-files Jul 10 Rui Barradas Extracting arithmetic mean for specific values from multiple .txt-files Jul 10 vimmster Extracting arithmetic mean for specific values from multiple .txt-files Jul 10 vimmster Extracting arithmetic mean for specific values from multiple .txt-files Jul 10 Rui Barradas Extracting arithmetic mean for specific values from multiple .txt-files Jul 11 vimmster Extracting arithmetic mean for specific values from multiple .txt-files Jul 11 vimmster Extracting arithmetic mean for specific values from multiple .txt-files Jul 12