[R] foor loop - undefined columns selected error

Michela Leonardi
Wed, 18 Nov 2009 14:58:19 -0000

Dear R-Help Members,

I am trying to read and analyse a set of 100 csv files.
I need work only on some columns in each of those, so I decided to use
a for loop, isolate the
column in each file to work on, but then an error mesage appear:
"undefined columns selected"

Here is my code:

setwd("F:/Data/")
a<-list.files()
for (x in a) {
? u<-read.csv(x, header=T, sep=",", check.names=FALSE)
#it give me the same problem using read.table
? h<-u[,2]
}

Error in `[.data.frame`(u, , 2) : undefined columns selected

It does not give me any problem selecting ane entire row (e.g. u[2,])
or a single value (e.g. [5,2])
If I try to select a column after the for loop I does not show any
problem, e.g.:

a<-list.files()
for (x in a) {
  u<-read.csv(x, header=T, sep=",", check.names=FALSE)
}
  h<-u[,2]

I would appreciate any suggestion or pointer to solve the problem or
to do the same thing in a different way.

Thanks for your consideration

--
Michela Leonardi