Hi, I have the following problem. In a csv file I have under column A, the date, and column B, the prices. Thus, for example, the file looks something like this -> 1/31/04 2.5 2/1/04 2.6 ... 4/12/04 3.5 Basically, I use the function inputframe = read.csv( ) which reads the csv file into the inputframe. My question is, how can I make a function that has, as inputs, start_date and end_date, so that the inputframe would not return all of these prices but only the ones during the period I input? For example, if I input 3/1/04 and 3/11/04, the inputframe only contains the prices for this period. Any help is appreciated!! Kim
Kim Fai Wong <gokim19 <at> hotmail.com> writes: : : Hi, : : I have the following problem. : In a csv file I have under column A, the date, and column B, the prices. : Thus, for example, the file looks something like this -> : : 1/31/04 2.5 : 2/1/04 2.6 : ... : 4/12/04 3.5 : : Basically, I use the function inputframe = read.csv( ) : which reads the csv file into the inputframe. : : My question is, how can I make a function that has, as inputs, start_date : and end_date, : so that the inputframe would not return all of these prices but only the : ones during the : period I input? : : For example, if I input 3/1/04 and 3/11/04, the inputframe only contains the : prices for : this period. : : Any help is appreciated!! : Note that read.csv produces data frames, not matrices. Lets assume your data frame is called DF. First make sure your A column is actually stored as some sort of date object and not as a factor or character string. You may need something like this if its not. (We are using chron here since m/d/y is its default format so its particularly consistent with your data.): library(chron) DF$A <- chron(as.character(DF$A)) Now use subset: DF2 <- subset(DF, A >= chron("3/1/04") & A <= chron("3/11/04")) You could alternately use Date class. See R News 4/1 for an article about all this.
This is about indexing a data frame: `matrix indexing' is something different. On Sun, 7 Nov 2004, Kim Fai Wong wrote:> Hi, > > I have the following problem. > In a csv file I have under column A, the date, and column B, the prices. > Thus, for example, the file looks something like this -> > > 1/31/04 2.5 > 2/1/04 2.6 > ... > 4/12/04 3.5 > > Basically, I use the function inputframe = read.csv( ) > which reads the csv file into the inputframe.Well, it isn't going to work as the file is not Comma Separated Values. Perhaps you need to look at ?read.table and tell us exactly what you propose to use. I am guessing the first column will become row names. When you have read this in, `inputframe' is a data frame, and not a matrix.> My question is, how can I make a function that has, as inputs, start_date > and end_date, > so that the inputframe would not return all of these prices but only the > ones during the > period I input? > > For example, if I input 3/1/04 and 3/11/04, the inputframe only contains the > prices for > this period.dates <- as.Date(row.names(inputframe), format="%m/%d/%y") inputframe[dates >= start_date & dates < end_date, ] where start_date and end_date are dates (not character strings in some strange non-ISO format).> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.htmlYour reading the posting guide and following its advice would be appreciated, including reading `An Introduction to R'. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595