I am totally new to R environment and I'm stuck at Date operations. The scenario is, I have a daily database of customer activity of a certain Store, and I need to extract last 30 months data starting from current date. In other words, suppose today is 18-NOV-2014, I need all the data from 18-OCT-2014 till today in a separate data-frame. To extract it, what kind of iteration logic should I write in R?
Asked
Active
Viewed 4,029 times
2
-
Please supply [a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including (a sample of) your data and any code you've already tried. – Thomas Nov 18 '14 at 10:18
-
And your desired output – David Arenburg Nov 18 '14 at 10:19
1 Answers
4
You don't need an iteration. What you could do is, assuming your data.frame is called X, and the date column, DATE, you could write:
X$DATE=as.Date(X$DATE, format='%d-%B-%Y')
the 'format' argument is to match your date format you specify in you question. Then, to get the lines you are interested in, something like:
X[X$DATE>=as.Date(today(),format='%d-%B-%Y')-30)]
which is all the lines that are after today - 30 days. Does this help at all?

Nikos
- 3,267
- 1
- 25
- 32
-
-
1
-
-
Apologies. David is right, sorry I had already loaded lubridate in my project. Sys.Date() is perhaps better (to use only base R). Thank you for mentioning it David. – Nikos Nov 18 '14 at 10:29
-