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

jim holtman jholtman at gmail.com
Mon Apr 23 19:59:40 CEST 2012


This might do it for you:


for (i in fileNames){
    input <- read.table(i, .....)
   # you might want to use regular expressions to extract just the date.
    input$fileName <- i
    write.table(i, ....)
}

On Mon, Apr 23, 2012 at 12:29 PM, 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.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.



More information about the R-help mailing list