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:
Dear Mr. Holtman,

but I cannot leave out the value and cannot change the values to 1200.995
manually (for each test subject with a reaction time > 1000 ms), because the
first your lead to incomplete data and the latter would be too
time-consuming.

Dear Rui,

here I have three files, which have exactly the same content as
"XYZ_34.txt", EXCEPT that the file "XYZ_50.txt" doesn't have a period in the
first value 1200.9952 IF YOU OPEN IT WITH THE EDITOR (!), maybe because I
didn't change the structure with MS Excel. The other two files should be
identical.

http://r.789695.n4.nabble.com/file/n4635962/XYZ_2.txt XYZ_2.txt
http://r.789695.n4.nabble.com/file/n4635962/XYZ_50.txt XYZ_50.txt
http://r.789695.n4.nabble.com/file/n4635962/XYZ_1112.txt XYZ_1112.txt

R gives me the following output:

fun <- function(x){
+     dat <- read.table(x, skip=14)
+     dat[ , 8] <- as.numeric(gsub("\\.", "", dat[, 8]))
+     mean(dat[, 8])
+ }

sapply(list.files(pattern="XYZ.*\\.txt"), fun)
XYZ_1112.txt    XYZ_2.txt   XYZ_50.txt
     345210.4     345210.4     310112.0

Your second suggestion leads to the same output:

fun <- function(x, skip = 14){
+     dat <- read.table(x, skip=skip)
+     dat[ , 8] <- as.numeric(gsub("\\.", "", dat[, 8]))
+     mean(dat[, 8])
+ }

sapply(list.files(pattern="XYZ.*\\.txt"), fun)
XYZ_1112.txt    XYZ_2.txt   XYZ_50.txt
     345210.4     345210.4     310112.0

Thank you for your replies!

Kind regards

--
View this message in context: 
http://r.789695.n4.nabble.com/Extracting-arithmetic-mean-for-specific-values-from-multiple-txt-files-tp4635809p4635962.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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.


______________________________________________
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