在我的分析中,我必须从Date对象中提取一周的日期和每两周的日期数。 我希望结果如下所示
Date weekday fortnightly 2010-05-01 Saturday 1 2010-05-02 Sunday 2 . . 2010-05-14 Friday 14 2010-05-15 Saturday 1 2010-05-16 Sunday 2 . . 2010-05-28 Friday 14 2010-05-29 Saturday 1 2010-05-30 Sunday 2 2010-05-31 Monday 3 2010-06-1 Tuesday 4 2010-06-2 Wednesday 5我提取工作日如下
df$weekday <- weekdays(as.Date(df$Date))但是,我不知道我可以使用哪个函数来提取每两周的日期编号。
任何建议,将不胜感激。
In my analysis, I have to extract the day of a week and the day number of each fortnight from Date objects. I want the result to look like below
Date weekday fortnightly 2010-05-01 Saturday 1 2010-05-02 Sunday 2 . . 2010-05-14 Friday 14 2010-05-15 Saturday 1 2010-05-16 Sunday 2 . . 2010-05-28 Friday 14 2010-05-29 Saturday 1 2010-05-30 Sunday 2 2010-05-31 Monday 3 2010-06-1 Tuesday 4 2010-06-2 Wednesday 5I extracted the weekday as below
df$weekday <- weekdays(as.Date(df$Date))However, I don't know which function I can use to extract the day number for each fortnight.
Any suggestions would be appreciated.
最满意答案
这是你想要的吗?
以@ akrun的数据为参考
v1 <- seq(as.Date('2010-05-01'), length.out=60, by = '1 day') c(rep(seq(1, 14, 1), floor(length(v1)/14)), 1:(length(v1)%%14)) # [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 # [33] 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4重复1到14的数字序列的length(v1)/14次,然后附加剩余的数字。
所以如果在你的情况下它是一个数据框,那么你可以尝试,
c(rep(seq(1, 14, 1), floor(nrow(df)/14)), 1:(nrow(df)%%14))这将再次产生相同的序列。
Is this what you want?
Taking @akrun's data as reference
v1 <- seq(as.Date('2010-05-01'), length.out=60, by = '1 day') c(rep(seq(1, 14, 1), floor(length(v1)/14)), 1:(length(v1)%%14)) # [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 # [33] 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4Repeating a sequence of numbers from 1 to 14 for length(v1)/14 times and then appending the remaining numbers.
So if in your case it's a data frame then you can try,
c(rep(seq(1, 14, 1), floor(nrow(df)/14)), 1:(nrow(df)%%14))which would again generate the same sequence.
从Date对象中提取两周的那一天?(Extract the day of fortnight from Date objects?)在我的分析中,我必须从Date对象中提取一周的日期和每两周的日期数。 我希望结果如下所示
Date weekday fortnightly 2010-05-01 Saturday 1 2010-05-02 Sunday 2 . . 2010-05-14 Friday 14 2010-05-15 Saturday 1 2010-05-16 Sunday 2 . . 2010-05-28 Friday 14 2010-05-29 Saturday 1 2010-05-30 Sunday 2 2010-05-31 Monday 3 2010-06-1 Tuesday 4 2010-06-2 Wednesday 5我提取工作日如下
df$weekday <- weekdays(as.Date(df$Date))但是,我不知道我可以使用哪个函数来提取每两周的日期编号。
任何建议,将不胜感激。
In my analysis, I have to extract the day of a week and the day number of each fortnight from Date objects. I want the result to look like below
Date weekday fortnightly 2010-05-01 Saturday 1 2010-05-02 Sunday 2 . . 2010-05-14 Friday 14 2010-05-15 Saturday 1 2010-05-16 Sunday 2 . . 2010-05-28 Friday 14 2010-05-29 Saturday 1 2010-05-30 Sunday 2 2010-05-31 Monday 3 2010-06-1 Tuesday 4 2010-06-2 Wednesday 5I extracted the weekday as below
df$weekday <- weekdays(as.Date(df$Date))However, I don't know which function I can use to extract the day number for each fortnight.
Any suggestions would be appreciated.
最满意答案
这是你想要的吗?
以@ akrun的数据为参考
v1 <- seq(as.Date('2010-05-01'), length.out=60, by = '1 day') c(rep(seq(1, 14, 1), floor(length(v1)/14)), 1:(length(v1)%%14)) # [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 # [33] 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4重复1到14的数字序列的length(v1)/14次,然后附加剩余的数字。
所以如果在你的情况下它是一个数据框,那么你可以尝试,
c(rep(seq(1, 14, 1), floor(nrow(df)/14)), 1:(nrow(df)%%14))这将再次产生相同的序列。
Is this what you want?
Taking @akrun's data as reference
v1 <- seq(as.Date('2010-05-01'), length.out=60, by = '1 day') c(rep(seq(1, 14, 1), floor(length(v1)/14)), 1:(length(v1)%%14)) # [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 # [33] 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4Repeating a sequence of numbers from 1 to 14 for length(v1)/14 times and then appending the remaining numbers.
So if in your case it's a data frame then you can try,
c(rep(seq(1, 14, 1), floor(nrow(df)/14)), 1:(nrow(df)%%14))which would again generate the same sequence.
发布评论