查找最近的星期一数据帧 [英] Find most recent Monday for a dataframe

查看:84
本文介绍了查找最近的星期一数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框对象,其中的字段中有一个日期:

  df $ dates 

我需要添加一个周开始列,即

  df [,'WeekStart'] = manipulation 

周的开始时间是该周的星期一的日期。即:今天是24/09/15,星期四,将有一个21的条目。下个星期二,01/10/15,将是'28 - 禁止'。



我看到有一个 weekday()函数将会将一天变成一个星期,但是如何处理这个最近的星期一

解决方案

Simples:

  dates< ;-( Sys.Date )+1:30)
week.start< - as.Date(sapply(dates,function(d){return(d +(-6 - as.POSIXlt(d)$ wday %% -7) )}),origin =1970-01-01)

并运行为

  d<  -  data.frame(dataes = dates,monday = week.starts)
/ pre>

  dataes monday 
1 2015 -09-25 2015-09-21
2 2015-09-26 2015-09-21
3 2015-09-27 2015-09-21
4 2015-09-28 2015 -09-28
5 2015-09-29 2015-09-28
6 2015-09-30 2015-09-28
7 2015-10-01 2015-09-28
8 2015-10-02 2015-09-28
9 2015-10-03 2015-09-28
10 2015-10-04 2015-09-28
11 2015-10-05 2015-10-05
12 2015-10-06 2015-10-05
13 2015-10-07 2015-10-05
14 2015-10- 08 2015-10-05
15 2015-10-09 2015-10-05
16 2015-10-10 2015-10-05
17 2015-10-11 2015-10- 05
18 2015-10-12 2015-10-12
19 2015-10-13 2015-10-12
20 2015-10-14 2015-10-12
21 2015-10-15 2015-10-12
22 2015-10-16 2015-10-12
23 2015-10-17 2015-10-12
24 2015-10- 18 2015-10-12
25 2015-10-19 2015-10-19
26 2015-10-20 2015-10-19
27 2015-10-21 2015-10- 19
28 2015-10-22 2015-10-19
29 2015-10-23 2015-10-19
30 2015-10-24 2015-10-19

类似的方法,例如:

 #data 
d< - data.frame(date = as.Date(c(20/09/2015,24/09/2015,28/09/2015, 01/10/2015),%d /%m /%Y))

#获取星期一
d $ WeekStart< - d $ date - 6 - (as.POSIXlt (d $ date)$ wday %% -7)

d
#result
#date WeekStart
#1 2015-09-20 2015-09-14
#2 2015-09-24 2015-09-21
#3 2015-09-28 2015-09-28
#4 2015-10-01 2015-09-28


I have a dataframe object, and among the fields in it, I have a dates:

df$dates

I need to add a column which is 'Week Starting', i.e.

df[,'WeekStart']= manipulation

Where the week start is the date of the Monday of that week. i.e.: today is Thursday 24/09/15, would have an entry as '21-Sept'. Next thursday, 01/10/15, would be '28-Sept'.

I see that there is a weekday() function which will convert a day into a week-day, but how can I deal with this most recent monday?

解决方案

Simples:

dates <-(Sys.Date()+1:30)
week.starts <- as.Date(sapply (dates, function(d) { return (d + (-6 - as.POSIXlt(d)$wday %% -7 ))}), origin = "1970-01-01")

and running as

d <- data.frame(dataes=dates, monday=week.starts)

gives

       dataes     monday
1  2015-09-25 2015-09-21
2  2015-09-26 2015-09-21
3  2015-09-27 2015-09-21
4  2015-09-28 2015-09-28
5  2015-09-29 2015-09-28
6  2015-09-30 2015-09-28
7  2015-10-01 2015-09-28
8  2015-10-02 2015-09-28
9  2015-10-03 2015-09-28
10 2015-10-04 2015-09-28
11 2015-10-05 2015-10-05
12 2015-10-06 2015-10-05
13 2015-10-07 2015-10-05
14 2015-10-08 2015-10-05
15 2015-10-09 2015-10-05
16 2015-10-10 2015-10-05
17 2015-10-11 2015-10-05
18 2015-10-12 2015-10-12
19 2015-10-13 2015-10-12
20 2015-10-14 2015-10-12
21 2015-10-15 2015-10-12
22 2015-10-16 2015-10-12
23 2015-10-17 2015-10-12
24 2015-10-18 2015-10-12
25 2015-10-19 2015-10-19
26 2015-10-20 2015-10-19
27 2015-10-21 2015-10-19
28 2015-10-22 2015-10-19
29 2015-10-23 2015-10-19
30 2015-10-24 2015-10-19

Similar approach, example:

# data
d <- data.frame(date = as.Date( c("20/09/2015","24/09/2015","28/09/2015","01/10/2015"), "%d/%m/%Y"))

# get monday
d$WeekStart <- d$date - 6 - (as.POSIXlt(d$date)$wday %% -7)

d
# result
#         date  WeekStart
# 1 2015-09-20 2015-09-14
# 2 2015-09-24 2015-09-21
# 3 2015-09-28 2015-09-28
# 4 2015-10-01 2015-09-28

这篇关于查找最近的星期一数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆