0

I've imported multiple files read.table and named each as file1, file2, file3,... I've put all of the data.frames (read.table files) into a list:

ClassFilter <- function(x) inherits(get(x), 'data.frame' )
Objs <- Filter( ClassFilter, ls() )

Now I want to call each data.frame (e.g. file1) and do a column mean:

for(x in 1:NumberOfFiles){
  mean[NumberOfFiles:400] <- apply(Objs[[x]],2,mean)
}

The problem is Objs[[x]] is giving me an error "Error in apply(Objs[[x]], 2, mean) : dim(X) must have a positive length" as the element in the list is not pointing to the data.frame. Anyone know how I can make the list element point to the data.frame from which the list was created initially? Thank you

crysis405
  • 1,121
  • 2
  • 13
  • 26
  • (1) I would investigate [reading the files directly](http://stackoverflow.com/q/5758084/324364) into a list, which bypasses the need for `get`. (2) Be careful using `apply` on data frames. If you have columns that aren't numbers, you won't like the result. – joran Dec 12 '12 at 18:22

1 Answers1

2

Objs contains the names of the data.frame variables, so you must get the variable first using get() function:

for(x in 1:NumberOfFiles){
  Mean[NumberOfFiles:400] <- apply(get(Objs[x]),2,mean)
}

or if you prefer you can also "evaluate" the variable name:

for(x in 1:NumberOfFiles){
  Mean[NumberOfFiles:400] <- apply(eval(as.name(Objs[x])),2,mean)
}

Also, as correctly pointed out by @SeñorO you should give a different name to mean object to avoid conflicts with the function mean.

digEmAll
  • 56,430
  • 9
  • 115
  • 140