[R] Argh! Trouble using string data read from a file

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Oct 16 00:07:10 CEST 2008


[[ ]] works on vectors!

> letters[[3]]
[1] "c"

See help("[[").  But strictly V4 is a factor and hence not a vector: [[ ]] 
also works on factors.

On Thu, 16 Oct 2008, Rolf Turner wrote:

>
> On 16/10/2008, at 10:03 AM, jim holtman wrote:
>
>> try putting as.character in the call:
>> 
>> x = read.csv(as.character(V4[[i]]), header = FALSE
>
> No.  This won't help.  V4 is a column of the data frame optdata,
> and hence is a vector.  Not a list!  Use single brackets --- V4[i] ---
> and all will be well.
>
> 	cheers,
>
> 		Rolf
>> 
>> On Wed, Oct 15, 2008 at 4:46 PM, Ted Byers <r.ted.byers at gmail.com> wrote:
>>> 
>>> Here is what I tried:
>>> 
>>> optdata =
>>> read.csv("K:\\MerchantData\\RiskModel\\AutomatedRiskModel\\soptions.dat",
>>> header = FALSE, na.strings="")
>>> optdata
>>> attach(optdata)
>>> for (i in 1:length(V4) ) { x = read.csv(V4[[i]], header = FALSE,
>>> na.strings="");x }
>>> 
>>> And here  is the outcome (just a few of the 60 records successfully read):
>>>> optdata =
>>>> read.csv("K:\\MerchantData\\RiskModel\\AutomatedRiskModel\\soptions.dat",
>>>> header = FALSE, na.strings="")
>>>> optdata
>>>   V1   V2 V3                        V4
>>> 1  251 2008 18 Plus_Shipping.2008.18.dat
>>> 2  251 2008 19 Plus_Shipping.2008.19.dat
>>> 3  251 2008 20 Plus_Shipping.2008.20.dat
>>> 4  251 2008 22 Plus_Shipping.2008.22.dat
>>> 5  251 2008 23 Plus_Shipping.2008.23.dat
>>> 6  251 2008 24 Plus_Shipping.2008.24.dat
>>> 
>>> I can see the data has been correctly read.  But for some reason that 
>>> isn't
>>> clear, read.csv doesn't like the data in the last column.
>>> 
>>>> attach(optdata)
>>>> for (i in 1:length(V4) ) { x = read.csv(V4[[i]], header = FALSE,
>>>> na.strings="");x }
>>> Error in read.table(file = file, header = header, sep = sep, quote = 
>>> quote,
>>> :
>>> 'file' must be a character string or connection
>>>> V4[[1]]
>>> [1] Plus_Shipping.2008.18.dat
>>> 60 Levels: Easyway.2008.17.dat Easyway.2008.18.dat Easyway.2008.19.dat
>>> Easyway.2008.20.dat ... Secured_Pay.2008.31.dat
>>> 
>>>> 
>>> 
>>> The last column is comprised of valid Windows filenames (and no 
>>> whitespace,
>>> so as not to confuse things).
>>> 
>>> I see in the docuentation "`[[...]]' is the operator used to select a 
>>> single
>>> element, whereas `[...]' is a general subscripting operator.", so I assume
>>> V4[[i]] is the correct way to get the ith value from V4.  So why does
>>> read.csv complain that "'file' must be a character string or connection"?
>>> It seems obvious that the value in V4[[i]i] is a string.  V4[[1]] does 
>>> give
>>> me the right value, although that is followed by output I didn't ask for.
>>> 
>>> In the loop above, I was going to replace the output obtained by 'x' with
>>> output from fitdistr(x,"exponential"), but I can't proceed with that until 
>>> I
>>> can get the data in these files read.
>>> 
>>> What have I missed?
>>> 
>>> Thanks
>>> 
>>> Ted
>>> --
>>> View this message in context: 
>>> http://www.nabble.com/Argh%21--Trouble-using-string-data-read-from-a-file-tp20002064p20002064.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>> 
>>> ______________________________________________
>>> 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.
>>> 
>> 
>> 
>> 
>> --
>> Jim Holtman
>> Cincinnati, OH
>> +1 513 646 9390
>> 
>> What is the problem that you are trying to solve?
>> 
>> ______________________________________________
>> 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.
>
>
> ######################################################################
> Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
>
> ______________________________________________
> 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.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list