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]]
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.
This little example might help.> foo <- data.frame(a=1:10, b=letters[1:0]) > fooa 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' > fooa 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.
Maybe Matching Threads
- Query about creating time sequences
- Query about memory used in list and dataframe
- Sum of column from another df based of row values of df1
- Problem with SQLDF - Error in sqliteExecStatement(con, statement, bind.data) : RS-DBI driver: (error in statement: no such table:
- Query regarding date as argument in functions - and about sqldf