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

2012-04-25 Thread Shivam
Thanks Jeff. I had tried the 'list' approach as well but got stuck with the below error: "Error in `$<-.data.frame`(`*tmp*`, "date", value = "20100701") : replacement has 1 rows, data has 0" Couldnt find a work around to this, hence resorted to the multiple dataframes approach. Any insights int

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

2012-04-24 Thread Jeff Newmiller
Programatically dealing with large numbers of separately-named objects leads to syntactically complicated code that is hard to read and maintain. Load the data frames into a list so you can access them by numeric or named index, and then getting at the loaded data will be much easier. fnames =

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

2012-04-24 Thread Shivam
Reposting in hope of a reply. On Tue, Apr 24, 2012 at 1:12 AM, Shivam wrote: > Thanks for the quick response. It works for an individual dataframe, but I > have many dataframes. This is the code so far > > fnames = list.files(path = getwd()) > for (i in 1:length(fnames)){ > assign(paste("file",i

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

2012-04-23 Thread Shivam
Thanks for the quick response. It works for an individual dataframe, but I have many dataframes. This is the code so far fnames = list.files(path = getwd()) for (i in 1:length(fnames)){ assign(paste("file",i,sep=""),read.csv.sql(fnames[i], sql = "select * from file where V3 == 'XXX' and V5=='YYY'"

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

2012-04-23 Thread MacQueen, Don
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 2012

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

2012-04-23 Thread jim holtman
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 wrote: > Hi, > > I am relatively new to R. Have