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

From: Uwe Ligges <ligges_at_statistik.uni-dortmund.de>
Date: Mon 28 Mar 2005 - 20:16:29 EST

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@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



R-help@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 Received on Mon Mar 28 20:33:38 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:56 EST