R语言 试图把不同年份的几天变成几周

vaj7vani  于 2023-05-20  发布在  其他
关注(0)|答案(2)|浏览(174)

我有数据。帧代表不同的年份。每年都有一个日期列。我想在每个变量中创建一个变量,它将一年中的前7天,第二个7天等分组。所以“2020-01-17”会在“01-15到01-21”中

#sample Data    
x1 <- data.frame(
    day=c("2020-02-21" ,"2020-01-19" ,"2020-01-30" ,"2020-01-17" ,"2020-02-18" ,"2020-02-31", "2020-02-21" ,"2020-01-02" ,"2020-01-28", "2020-02-27" ,"2020-02-29","2020-02-11" ,"2020-01-05", "2020-02-06", "2020-02-10", "2020-01-31" ,"2020-02-18"),
    one = 1 )
    
    x2 <- data.frame(
    day=c("2021-02-21" ,"2021-01-19" ,"2021-01-30" ,"2021-01-17" ,"2021-02-18" ,"2021-02-31", "2021-02-21" ,"2021-01-02" ,"2021-01-28", "2021-02-27" ,"2021-02-29","2021-02-11" ,"2021-01-05", "2021-02-06", "2021-02-10", "2021-01-31" ,"2021-02-18"),
    one = 1 )

我知道如何将天转换为周,但是如果我按照建议使用formatRemove year from dates in R)删除年,它会生成一个字符,然后我就不能使用cut了。

x2$day <- as.Date( x2$day , "%Y-%m-%d") 
x1$day <- as.Date( x1$day , "%Y-%m-%d") 
x1$day2 <- format( x1$day , "%m-%d")
class( x1$day2)

如果我不放弃年份,那么同一日期将在不同的星期结束。

using the cut function "2020-02-21", and "2021-02-21" are in different weeks.  I want them to be in the same bin

cut(as.Date(x2$day), breaks="week")
cut(as.Date(x1$day), breaks="week")
of1yzvn4

of1yzvn41#

如果这包括闰年和非闰年,那就不起作用了,因为一年中的第i天可能落在不同的日期范围内。我们能做的就是根据组号命名组。让我们把一年中的前14天记为0,接下来的14天记为1,以此类推。

transform(x1, g = as.POSIXlt(day, format = "%Y-%m-%d")$yday %/% 14)

给出:

day one  g
1  2020-02-21   1  3
2  2020-01-19   1  1
3  2020-01-30   1  2
4  2020-01-17   1  1
5  2020-02-18   1  3
6  2020-02-31   1 NA
7  2020-02-21   1  3
8  2020-01-02   1  0
9  2020-01-28   1  1
10 2020-02-27   1  4
11 2020-02-29   1  4
12 2020-02-11   1  2
13 2020-01-05   1  0
14 2020-02-06   1  2
15 2020-02-10   1  2
16 2020-01-31   1  2
17 2020-02-18   1  3
dxxyhpgq

dxxyhpgq2#

你在正确的道路上与cut()。使用跨越全年的每周序列,可以使用findInterval()来查找与每个日期匹配的序列索引(即周数)。

library(dplyr)
library(lubridate)
x1 <- data.frame(
  day=ymd(c("2020-02-21" ,"2020-01-19" ,"2020-01-30" ,"2020-01-17" ,"2020-02-18" ,"2020-02-31", "2020-02-21" ,"2020-01-02" ,"2020-01-28", "2020-02-27" ,"2020-02-29","2020-02-11" ,"2020-01-05", "2020-02-06", "2020-02-10", "2020-01-31" ,"2020-02-18")),
  one = 1 )
#> Warning: 1 failed to parse.

x2 <- data.frame(
  day=ymd(c("2021-02-21" ,"2021-01-19" ,"2021-01-30" ,"2021-01-17" ,"2021-02-18" ,"2021-02-31", "2021-02-21" ,"2021-01-02" ,"2021-01-28", "2021-02-27" ,"2021-02-29","2021-02-11" ,"2021-01-05", "2021-02-06", "2021-02-10", "2021-01-31" ,"2021-02-18")),
  one = 1 )
#> Warning: 2 failed to parse.

x1seq <- seq(dmy("01-01-2020"), dmy("31-12-2020"), by = "7 days")
x2seq <- seq(dmy("01-01-2021"), dmy("31-12-2021"), by = "7 days")
x1 %>%
  mutate(week_start = x1seq[findInterval(day, x1seq,
                                         rightmost.closed = FALSE, 
                                         left.open = FALSE, 
                                         all.inside = FALSE)])
#>           day one week_start
#> 1  2020-02-21   1 2020-02-19
#> 2  2020-01-19   1 2020-01-15
#> 3  2020-01-30   1 2020-01-29
#> 4  2020-01-17   1 2020-01-15
#> 5  2020-02-18   1 2020-02-12
#> 6        <NA>   1       <NA>
#> 7  2020-02-21   1 2020-02-19
#> 8  2020-01-02   1 2020-01-01
#> 9  2020-01-28   1 2020-01-22
#> 10 2020-02-27   1 2020-02-26
#> 11 2020-02-29   1 2020-02-26
#> 12 2020-02-11   1 2020-02-05
#> 13 2020-01-05   1 2020-01-01
#> 14 2020-02-06   1 2020-02-05
#> 15 2020-02-10   1 2020-02-05
#> 16 2020-01-31   1 2020-01-29
#> 17 2020-02-18   1 2020-02-12
x2 %>%
  mutate(week_start = x2seq[findInterval(day, x2seq,
                                         rightmost.closed = FALSE, 
                                         left.open = FALSE, 
                                         all.inside = FALSE)])
#>           day one week_start
#> 1  2021-02-21   1 2021-02-19
#> 2  2021-01-19   1 2021-01-15
#> 3  2021-01-30   1 2021-01-29
#> 4  2021-01-17   1 2021-01-15
#> 5  2021-02-18   1 2021-02-12
#> 6        <NA>   1       <NA>
#> 7  2021-02-21   1 2021-02-19
#> 8  2021-01-02   1 2021-01-01
#> 9  2021-01-28   1 2021-01-22
#> 10 2021-02-27   1 2021-02-26
#> 11       <NA>   1       <NA>
#> 12 2021-02-11   1 2021-02-05
#> 13 2021-01-05   1 2021-01-01
#> 14 2021-02-06   1 2021-02-05
#> 15 2021-02-10   1 2021-02-05
#> 16 2021-01-31   1 2021-01-29
#> 17 2021-02-18   1 2021-02-12

创建于2023-05-17带有reprex v2.0.2

相关问题