Andrew Peterson
2005-Mar-28 02:59 UTC
[R] Can I use a variable to pass a file name to scan() ?
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.
Spencer Graves
2005-Mar-28 03:35 UTC
[R] Can I use a variable to pass a file name to scan() ?
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
>
>
Andrew Peterson wrote:> Hi, can anyone please tell me if it is possible to use > a variable to pass a file name to scan()?Yes.> 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>"Right, the file named in x[1] is a text file that starts with "<CLOSE>" which is not a number (and scan expects a number, by default), hence scan complains. Use y <- scan(file = x[1], what="character") and you will see that it works. Uwe Ligges> 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