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

vimmster superdodge at gmx.net
Tue Jul 10 10:35:53 CEST 2012


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.



More information about the R-help mailing list