[R] How to insert filename as column in a file

MacQueen, Don macqueen1 at llnl.gov
Mon Apr 23 20:31:25 CEST 2012


This little example might help.

> foo <- data.frame(a=1:10, b=letters[1:0])
> foo
    a b
1   1 a
2   2 a
3   3 a
4   4 a
5   5 a
6   6 a
7   7 a
8   8 a
9   9 a
10 10 a
> foo$date <- '20120423'
> foo
    a b     date
1   1 a 20120423
2   2 a 20120423
3   3 a 20120423
4   4 a 20120423
5   5 a 20120423
6   6 a 20120423
7   7 a 20120423
8   8 a 20120423
9   9 a 20120423
10 10 a 20120423


In other words, immediately after reading the data into a data frame, add
a date column as in the example. You'll have to extract the date from the
filename, of course.

-Don


-- 
Don MacQueen

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





On 4/23/12 9:29 AM, "Shivam" <shivamsingh at gmail.com> wrote:

>Hi,
>
>I am relatively new to R. Have scourged the help files and the www but
>havent been able to get a solution.
>
>I have around 250 csv files, one file for each date. They have columns of
>all types, numeric, string etc. The name of each file is the date in the
>form of 'yyyymmdd'. There is no column within the file which helps me
>identify the date on which the file was generated, only the filename has
>that info.
>
>I am selecting some data (using read.csv.sql) from each file and creating
>a
>dataset for each day. Ultimately I will combine all the datasets. I can
>accomplish the select and combine part, but after combining I wont have a
>record as to the date corresponding to the data.
>
>Hence I want to insert the filename as a column in the respective file to
>help me in identifying to what date each data row belongs to.
>
>Sorry for the long mail, but wanted to make myself clear. Any help would
>be
>greatly appreciated.
>
>Thanks in advance,
>Shivam
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list