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

Bert Gunter gunter.berton at gene.com
Thu Jul 26 17:10:28 CEST 2012


Sorry, got cut off...

On Thu, Jul 26, 2012 at 8:07 AM, Bert Gunter <bgunter at gene.com> wrote:
> 1. m2q already is a data frame, so the m2qldf statement that follows
> is completely unnecessary.
>
> 2. Please read ?read.table carefully, and especially the bit about the
> stringsAsFactors argument. The problem is that by default character
> strings are read in as factors, not characters strings. So you can
> either change the way they are read in thru stringsAsFactors=FALSE or
> otherwise as explained therein -- or convert them with as.character()
> when you access them, e.g.

 x1<-read.csv(file=as.character(m2q[1,2]))

-- Bert

>
> On Thu, Jul 26, 2012 at 7:53 AM, jpm miao <miaojpm at gmail.com> wrote:
>> 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
>>   # 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
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> --
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> Internal Contact Info:
> Phone: 467-7374
> Website:
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



More information about the R-help mailing list