简体   繁体   English

从R数据框中提取过去30天的全天数据

[英]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. 我对R环境完全陌生,并且无法进行Date操作。 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. 场景是,我有某个商店的客户活动的每日数据库,我需要提取从当前日期开始的最近30个月的数据。 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. 换句话说,假设今天是2014年11月18日,我需要在一个单独的数据帧中获取2014年10月18日之前的所有数据。 To extract it, what kind of iteration logic should I write in R? 要提取它,我应该在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: 您可以做的是,假设您的data.frame称为X,并且日期列DATE,您可以编写:

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. 这是今天之后的所有行-30天。 Does this help at all? 这有帮助吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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