[R] extracting file names

MacQueen, Don macqueen1 at llnl.gov
Fri Mar 25 21:12:27 CET 2011


While I think David's suggestion is better, because it's more readable,
this should also work:

  dat <- read.table(file.name <- file.choose(), header = FALSE)

Note the assignment inside the function call.

-Don

-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 3/23/11 8:49 PM, "David Wolfskill" <r at catwhisker.org> wrote:

>On Thu, Mar 24, 2011 at 03:49:32PM +1300, Darcy Webber wrote:
>> Dear R users,
>> 
>> I am trying to figure out a way to extract the original file name of a
>> .DAT (e.g., IC48.DAT) file imported into R using the file.choose()
>> function, i.e.,
>> 
>> dat <- read.table(file.choose(), header = FALSE)
>> 
>> The reason I would like to do this is to use that file name to name an
>> output file, thus if I had the file name, e.g.,
>> 
>> file.name <- IC48
>> # then I do a bunch of stuff to dat
>> 
>> Then I want to save it as:
>> 
>> write.table(dat, file = paste(file.name"_output.DAT"))
>> 
>> Does anyone know any way I could do this. Thanks in advance.
>
>Per ?file.choose, output of file.choose() is "A character vector
>of length one giving the file path," so why not do that separately
>from the read.table(), such as:
>
>file.name <- file.choose()
>dat <- read.table(file.name, header = FALSE)
>...
>write.table(dat, file = paste(file.name, "_output.DAT", sep = ""))
>
>[Caveat: I haven't actually tried the above.]
>
>> ...
>
>Peace,
>david
>-- 
>David H. Wolfskill                r at catwhisker.org
>Depriving a girl or boy of an opportunity for education is evil.
>
>See http://www.catwhisker.org/~david/publickey.gpg for my public key.



More information about the R-help mailing list