简体   繁体   中英

Extract data for all days for last 30 days from R data frame

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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM