Skip to content
Prev 299568 / 398503 Next

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

Hello,

There must be a difference in the file you are processing and in the one 
excel and I are:


 > 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_34.txt
   345210.4

This result is even better, more accurate than excel's.

AS for the second question, because with the dots, those values are read 
by R as character and when put into the data.frame converted to factors, 
the name R gives to categorical variables. You can see this with the 
instruction, right after the read.table,

print(str(dat))
 > str(dat)
'data.frame':   151 obs. of  8 variables:
  $ V1: int  2 2 2 2 2 2 2 2 2 2 ...
  $ V2: int  1 2 3 4 5 6 7 8 9 10 ...
  $ V3: int  1 2 3 4 5 6 7 8 9 10 ...
  $ V4: int  3 2 4 3 3 1 3 1 3 2 ...
  $ V5: int  27 16 16 27 27 27 27 27 27 16 ...
  $ V6: int  0 16 16 16 27 27 27 27 16 16 ...
  $ V7: int  6 1 1 2 1 1 1 1 2 1 ...
  $ V8: Factor w/ 151 levels "1.200.995","247.102",..: 1 139 135 39 133 
73 142 63 77 67 ...

It's V8 the column we want. The real values are 1 139 135 39 etc. The 
levels are categories labels, the categories themselves are the 1-based 
integer values.

Anyway, what's important is that the code is working, and if there's an 
error maybe it can be solved with this modification:


fun <- function(x, skip = 14){
	dat <- read.table(x, skip=skip)

And the rest is the same. Inspect the file and see if the data starts at 
line 15.

(And please, Rui is enough, NO 'Mr.')

Hope this helps,

Rui Barradas

Em 09-07-2012 14:54, 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