Can I use a variable to pass a file name to scan() ?
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