简体   繁体   中英

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. Likewise 2012044 means 4th week of April,2012. I was trying to make the values of date expressing some order. 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.

My goal is this: Find the oldest date in date column and name it as 1. In this case, 2012034 should be 1 . Next, find the second oldest date in date column and calculate how many weeks passed after that date . The second oldest date in date is 2012044 .So, 5 weeks after the oldest date 2012034 . So it should be changed as 1+5=6. So, likewise, I want to number the date to indicate how many weeks have passed since the oldest 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"

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