[R] Can I use a variable to pass a file name to scan() ?

Spencer Graves spencer.graves at pdf.com
Mon Mar 28 05:35:49 CEST 2005


      What operating system and which version of R? 

      I got a similar error message under R 2.0.1 under Windows 2000: 

 > y <- scan(file = x[1])
Error in scan(file = x[1]) : "scan" expected a real, got "ÐÏࡱ"

      Part of the function definition for "scan" is 'scan(file = "", 
what = double(0), ...'.  This means that by default, "scan" was 
expecting double precision numbers.  When it found an unrecognizable 
binary, it complained.  Using 'what="raw"' produced something: 

 > y <- scan(file = x[1], what="raw")
Read 1 items

      The file x[1] was an MS Word *.wbk file, and scan was only able to 
read a portion of the header using "raw". 

      Have you considered "try"? 

 > y <- try(scan(file=x[1]))
Error in scan(file = x[1]) : "scan" expected a real, got "ÐÏࡱ"
 > y
[1] "Error in scan(file = x[1]) : \"scan\" expected a real, got 
\"ÐÏ\021à¡\261\"\n" 
attr(,"class")
[1] "try-error"

       What are you trying to do?  The following worked for me just now? 

 > write(pi, "pi.txt")
 > scan('pi.txt')
Read 1 items
[1] 3.141593
 > all.equal(scan('pi.txt'), pi)
Read 1 items
[1] "Mean relative  difference: 1.102658e-07"

      hope this helps.  spencer graves

Andrew Peterson wrote:

>Hi, can anyone please tell me if it is possible to use
>a variable to pass a file name to scan()?
>
>I am trying to write a program that loops through a
>series of text files. I use list.files() to create a
>character vector of file names. Then I would like to
>take  one string at a time from the character vector
>and use that string to specify the name of the text
>file I want to open using scan().
>
>I have used constructs such as
>
>x <- list.files()
>
>y <- scan(file = x[1])
>
>but I always get the following error message:
>
>Error in scan(file = x[1]) : "scan" expected a real,
>got "<CLOSE>"
>
>I've read all the documentation at least a couple of
>times and have searched this list to no avail. Any
>help would be greatly appreciated!
>
>Thanks,
>
>Andrew.
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>  
>




More information about the R-help mailing list