简体   繁体   English

年月周表达式

[英]Year-month-week expression

I have a data written in specific expression.我有一个用特定表达式写的数据。 To simplify the data, here is the example I made:为了简化数据,这里是我做的例子:

df<-data.frame(date=c(2012034,2012044,2012051,2012063,2012074),
               math=c(100,100,23,46,78))

2012034 means 4th week of march,2012. 2012034表示 2012 年 3 月的第 4 周。 Likewise 2012044 means 4th week of April,2012.同样, 2012044表示 2012 年 4 月的第 4 周。 I was trying to make the values of date expressing some order.我试图使date的值表达某种顺序。 The reason why I have to do this is because when I don't change them to time expressions, x axis of the scatter plot looks really weird.我之所以必须这样做是因为当我不将它们更改为时间表达式时,散点图的 x 轴看起来很奇怪。

My goal is this: Find the oldest date in date column and name it as 1. In this case, 2012034 should be 1 .我的目标是:在date列中找到最旧的日期并将其命名为 1。在这种情况下, 2012034应该是1 Next, find the second oldest date in date column and calculate how many weeks passed after that date .接下来,在date列中找到第二个最旧的日期,并计算该date之后经过了多少周。 The second oldest date in date is 2012044 .So, 5 weeks after the oldest date 2012034 . date 中第二旧的date2012044 。因此,在最旧日期2012034之后 5 周。 So it should be changed as 1+5=6.所以应该改成1+5=6。 So, likewise, I want to number the date to indicate how many weeks have passed since the oldest date所以,同样,我想给date编号,以表明距离最早的date已经过去了多少周

One way to do it is by also specifying the day of the week and subtract it at the end, ie一种方法是同时指定星期几并在最后减去它,即

as.Date(paste0(df$date, '-1'), '%Y%m%U-%u') - 1
#[1] "2012-03-22" "2012-04-22" "2012-05-01" "2012-06-15" "2012-07-22"

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

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