[R] How can I correctly access a string element of a dataframe?

jpm miao miaojpm at gmail.com
Thu Jul 26 16:53:39 CEST 2012


Dear All,



  I would like to read the data file via read.csv (the 3rd line of the
following program) and the file name is stored in a dataframe. Since I have
several files to read, I store the file names as well as the sample period
inside a file “B_M2Q.csv” and I read the file name first, and then read the
data file.



  The content of the file "B_M2Q.csv" is

  FX

"A_FX_M.csv"

"1981-01"

"2012-06"

Int

"A_Int.csv"

"1970-01"

"2012-03"

  (The file is attached to this email)


  The three-line program is as follows:


m2q<-read.csv(file="B_M2Q.csv", header=FALSE)  # Read the file containing
file names and sampling periods of numerical data

m2qdf<-as.data.frame(m2q)   #Make the file of filenames a dataframe

x1<-read.csv(file=m2qdf[1,2])   # Read the numerical data

Nevertheless, there is an error when I  attempt to read the numerical data



> x1<-read.csv(file=m2qdf[1,2])

Error in read.table(file = file, header = header, sep = sep, quote = quote,
:

  'file' must be a character string or connection

It is a problem of the file name , since I can read the data smoothly if I
change the line to

> x1<-read.csv(file="A_FX_M.csv")

Could someone tell me what the problem is?
How can modify the orignal program so that I can store the sampling periods
and filenames in a dataframe, and then I read it?
How can I correctly access a string element of a dataframe?

Thanks,

Miao


More information about the R-help mailing list