本文介绍了在星期天开始一周的第一天的第一天,并在星期六结束一周的最后一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我最近在R处理日期时遇到了一个问题。 2015年(2015-12-31)的最后一天是星期四,这意味着如果我认为星期日是我这周的开始日,那年的上周只会包含5天。现在,我希望周五和周六的第一周一至十一日和第011-02周,与周五相关,周一至周一,周一至周一。 require(lubridate) range< - seq(as.Date('2015-12-26'),by = 1,len = 10) df< - data.frame(range) df $ WKN df $ weekday< - 工作日(df $ range) df $ weeknum< - wday(df $ range) 这将给我以下结果: df: range WKN工作日weeknum 2015-12-26 52星期六7 2015-12-27 53星期日1 2015-12-28 53星期一2 2015-12-29 53星期二3 2015-12-30 53星期三4 2015-12-31 53周四5 2016-01-01 1星期六6 2016-01-02 1星期六7 2016-01-03 2星期日1 2016-01-04 2星期一2 现在我想有我的数据框如下: df:范围WKN平日weeknum 2015-12-26 52星期六7 2015-12-27 53星期日1 2015-12-28 53星期一2 2015-12 -29 53星期二3 2015-12-30 53星期三4 2015-12-31 53星期四5 2016-01-01 53星期六6 2016-01-02 53星期六7 2016-01-03 1星期日1 2016-01-04 1星期一2 任何人都可以指出自动化的方向,以便每年都不需要修改代码?解决方案如果您查看?strptime ,则可以使用与格式。这里%V 几乎可以工作,除了它在星期一开始,所以添加一个调整: df $ WKN df ##范围WKN工作日周 ## 1 2015-12-26 52星期六7 ## 2 2015-12-27 53星期日1 ## 3 2015-12-28 53星期一2 # #4 2015-12-29 53 Tuesday 3 ## 5 2015-12-30 53 Wednesday 4 ## 6 2015-12-31 53星期四5 ## 7 2016-01 -01 53星期五6 ## 8 2016-01-02 53星期六7 ## 9月1月3日1星期日1 ## 10 2016-01-04 1星期一2 或者如果你像标签那样使用dplyr, library(dplyr) df%>%mutate(WKN = as.integer(format(range + 1,'%V' ))) 返回相同的事情。 lubridate的 isoweek 功能是等效的,所以你也可以做 图书馆(lubridate) df $ WKN< - isoweek(df $ range + 1) 或 df%>%mutate(WKN = isoweek(range + 1)) 两者都返回相同的结果到 as.integer(format(...)) 版本。 I have recently encountered a problem in R dealing with the dates. The last day of 2015 (2015-12-31) falls on Thursday, meaning last week of the year only contains 5 days if I consider Sunday as the start day of my week. Now, I would like 2016-01-01 and 2016-01-02, which fall on Friday and Saturday, to be associated with week 53, and start week 1 on 2016-01-03, which falls on Sunday.require(lubridate)range <- seq(as.Date('2015-12-26'), by = 1, len = 10)df <- data.frame(range)df$WKN <- as.numeric(strftime(df$range, format = "%U")) + 1df$weekday <- weekdays(df$range)df$weeknum <- wday(df$range)This would give me the following result:df:range WKN weekday weeknum2015-12-26 52 Saturday 72015-12-27 53 Sunday 12015-12-28 53 Monday 22015-12-29 53 Tuesday 32015-12-30 53 Wednesday 42015-12-31 53 Thursday 52016-01-01 1 Friday 62016-01-02 1 Saturday 72016-01-03 2 Sunday 12016-01-04 2 Monday 2Now I would like to have my dataframe as follows:df:range WKN weekday weeknum2015-12-26 52 Saturday 72015-12-27 53 Sunday 12015-12-28 53 Monday 22015-12-29 53 Tuesday 32015-12-30 53 Wednesday 42015-12-31 53 Thursday 52016-01-01 53 Friday 62016-01-02 53 Saturday 72016-01-03 1 Sunday 12016-01-04 1 Monday 2Could anyone point me to a direction to automate that so that I don't have to change the code every year? 解决方案 If you check out ?strptime, there are a few different week number tokens available for use with format. Here %V almost works, except it starts the week on Monday, so add one to adjust:df$WKN <- as.integer(format(df$range + 1, '%V'))df## range WKN weekday weeknum## 1 2015-12-26 52 Saturday 7## 2 2015-12-27 53 Sunday 1## 3 2015-12-28 53 Monday 2## 4 2015-12-29 53 Tuesday 3## 5 2015-12-30 53 Wednesday 4## 6 2015-12-31 53 Thursday 5## 7 2016-01-01 53 Friday 6## 8 2016-01-02 53 Saturday 7## 9 2016-01-03 1 Sunday 1## 10 2016-01-04 1 Monday 2Or if you're using dplyr like the tag suggests,library(dplyr)df %>% mutate(WKN = as.integer(format(range + 1, '%V')))which returns the same thing. The isoweek function of lubridate is equivalent, so you could also dolibrary(lubridate)df$WKN <- isoweek(df$range + 1)ordf %>% mutate(WKN = isoweek(range + 1))both of which return identical results to the as.integer(format(...)) versions. 这篇关于在星期天开始一周的第一天的第一天,并在星期六结束一周的最后一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-24 00:28