[R] Fwd: Error in read.table

peter dalgaard pdalgd at gmail.com
Thu Jul 11 16:56:02 CEST 2013


On Jul 11, 2013, at 14:40 , Jannetta Steyn wrote:

> Maybe I should rephrase my question. What would be the best way to read a
> list of filenames and headings from a csv file?

Just follow the hints already given, I think. E.g

files <- read.csv(file="files.csv", head=FALSE, sep=",", as.is=TRUE)

# for each filename read the file
for (i in 1:length(files)) {
   fn <- files[i,1] # Alias files[i,][[1]]
   head <- files[i,2]
   con<-read.csv(file=fn, header=TRUE, sep=',')
...

You can get fancy and do things with apply(files, 1, FUN=.....), but it's probably not necessary.


> The CSV file is structured as two columns, with column one being the
> filename and column two being a heading e.g.:
> ANA110915004A_3PERIOD_TmAvg-rdata.csv,Pre-DA
> ANA110915006A_3PERIOD_TmAvg-rdata.csv,DA-10^-6
> ANA110915012A_3PERIOD_TmAvg-rdata.csv,DA-10^-4
> ANA110915016A_3PERIOD_TmAvg-rdata.csv,Washout
> 
> 
> I want to be able to open the file using read.csv and use the heading as
> the header of a graph.
> 
> Reading the filenames from the directory with list.files() works but then I
> don't have the headings that go with the file e.g.:
> filenames<-list.files(pattern="*.csv")
> for (i in seq_along(filenames)) {
>  con<-read.csv(filenames[i], headers=TRUE, sep=',')
> }
> 
> Regards
> Jannetta
> 
> 
> On 11 July 2013 13:27, peter dalgaard <pdalgd at gmail.com> wrote:
> 
>> 
>> On Jul 11, 2013, at 13:50 , Dante.py wrote:
>> 
>>> Maybe you should set parameter "as.is" in read.csv to be false.
>> 
>> TRUE more likely... However, there's another issue: files[i,] is a data
>> frame, therefore so is f[1] (a telltale sign is that it has rows and column
>> labels). So f[[1]] is probably needed.
>> 
>>> 
>>> 
>>> 2013/7/11 Jannetta Steyn <jannetta at henning.org>
>>> 
>>>> Hi Folks
>>>> 
>>>> I can't see what I have done wrong in this piece of code.
>>>> 
>>>> # Read filenames from csv file
>>>> files <- read.csv(file="files.csv",head=FALSE,sep=",")
>>>> 
>>>> # for each filename read the file
>>>> for (i in 1:length(files)) {
>>>> # f becomes the next row inthe file
>>>> f<-files[i,]
>>>> # the header to be used for the graph is in column 2 of f
>>>> head=f[2]
>>>> par(mfrow=c(4,2))
>>>> # the filename to be used is in column 1 of f
>>>> con<-read.csv(file=f[1], header=TRUE, sep=',')
>>>> tmp<-con$value2
>>>> data<-normalize_js(tmp,-1,1)
>>>> time<-con$time
>>>> # run the waveform analyser
>>>> waveformanalyser(data,time,head)
>>>> }
>>>> 
>>>> I get an error:
>>>> Error in read.table(file = file, header = header, sep = sep, quote =
>> quote,
>>>> :
>>>> 'file' must be a character string or connection
>>>> 
>>>> When the error occurs f equals:
>>>>> f
>>>>                                    V1     V2
>>>> 1 ANA110915004A_3PERIOD_TmAvg-rdata.csv Pre-DA
>>>> 
>>>> and f[1] equals:
>>>> 
>>>>> f[1]
>>>>                                    V1
>>>> 1 ANA110915004A_3PERIOD_TmAvg-rdata.csv
>>>> 
>>>> 
>>>> Why won't it use the value of f[1] as a string for the file parameter in
>>>> the read.csv line?
>>>> 
>>>> All help would be apprecitated.
>>>> Regards
>>>> Jannetta
>>>> 
>>>> 
>>>> 
>>>> --
>>>> 
>>>> ===================================
>>>> Web site: http://www.jannetta.com
>>>> Email: jannetta at henning.org
>>>> ===================================
>>>> 
>>>>       [[alternative HTML version deleted]]
>>>> 
>>>> ______________________________________________
>>>> 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.
>>>> 
>>> 
>>> 
>>> 
>>> --
>>> ÕÅêÊ Dante.py
>>> Ö‹É∏´óѧ09π¶Ê‡Ñ§ÓëÓœÓÃʇѧ◊¨Òµ
>>> ¸öÈËÖ÷Ò“£ºhttp://dantepy.yslsg.org/
>>> 
>>>      [[alternative HTML version deleted]]
>>> 
>>> ______________________________________________
>>> 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.
>> 
>> --
>> Peter Dalgaard, Professor,
>> Center for Statistics, Copenhagen Business School
>> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
>> Phone: (+45)38153501
>> Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
> 
> 
> -- 
> 
> ===================================
> Web site: http://www.jannetta.com
> Email: jannetta at henning.org
> ===================================
> 
> 
> 
> -- 
> 
> ===================================
> Web site: http://www.jannetta.com
> Email: jannetta at henning.org
> ===================================
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list