如何根据现有日期创建一系列年 - 周字符串值?

问题描述 投票:2回答:4

我正在绘制从2018年到2019年的每周数字,我的X轴上的刻度线代表当年的一年。

例如:

2018-50, 2018-51, 2018-52, 2018-53, 2019-01, 2019-02, 2019-03

我有两个数据框,其中任何一个的日期并不总是相同。因此,我认为可能有效的一个解决方案是在任一数据帧中找到最低的yearWeek值,并在任一数据帧中找到最大的yearWeek值,然后使用这两个值创建序列。请注意,两个值都可以存在于单个数据帧中,或者一个数据帧可以具有最低/最早值,另一个数据帧可以具有最高/最新值。

两个数据框都如下所示:

  week yearWeek      month  day       date
1   31  2018-31 2018-08-01  Wed 2018-08-01
2   31  2018-31 2018-08-01  Thu 2018-08-02
3   31  2018-31 2018-08-01  Fri 2018-08-03
4   31  2018-31 2018-08-01  Sat 2018-08-04
5   32  2018-32 2018-08-01  Sun 2018-08-05
6   32  2018-32 2018-08-01  Mon 2018-08-06

我找了一个解决方案,this answer几乎就在那里,但并不完全。

这个解决方案的问题是:

  • 单个数字的周数在它们之前没有0;和
  • 尽管指定seq(31:53),例如,输出从1开始(我知道为什么会发生这种情况);和
  • 似乎没有办法在53使用这种方法阻止计数(2018有一个(短)第53周,我想包括)和从2019-01恢复。

我希望能够设置从2018-31(2018年第31周)到2019-13(2019年第13周)的X轴范围。

像这样的东西:

enter image description here

简而言之,如何创建一系列年 - 周值,从最小日期值到最大日期值(在本例中为2018-31-2019-13)?

r sequence
4个回答
1
投票

我认为这对你有用

x1 <- c(31:53)
x2 <- sprintf("%02d", c(1:13))
paste(c(rep(2018, length(x1)), rep(2019, length(x2))), c(x1, x2), sep = "-")

# [1] "2018-31" "2018-32" "2018-33" "2018-34" "2018-35" "2018-36" "2018-37" 
#     "2018-38" "2018-39" "2018-40" "2018-41" "2018-42" "2018-43" "2018-44" 
#     "2018-45" "2018-46" "2018-47" "2018-48" "2018-49" "2018-50" "2018-51" 
#     "2018-52" "2018-53" "2019-01" "2019-02" "2019-03" "2019-04" "2019-05" 
# "2019-06" "2019-07" "2019-08" "2019-09" "2019-10" "2019-11" "2019-12" "2019-13"

对于我们可以做的更新问题

#rbind both the dataset
df <- rbind(df1, df2)

#convert them to date
df$Date <- as.Date(df$date)

#Generate a sequence from min date to maximum date, format them 
# to year-week combination and select only the unique ones
unique(format(seq(min(df$Date), max(df$Date), by = "day"), "%Y-%W"))

0
投票

定义两个序列,然后限制到您想要的范围:

years <- c("2018", "2019")
months <- sprintf("%02d", c(1:52))

result <- apply(expand.grid(years, months), 1, function(x) paste(x,collapse="-"))
result <- result[result >= "2018-31" & result <= "2019-13"]
result

 [1] "2019-01" "2019-02" "2019-03" "2019-04" "2019-05" "2019-06" "2019-07"
 [8] "2019-08" "2019-09" "2019-10" "2019-11" "2019-12" "2019-13" "2018-31"
[15] "2018-32" "2018-33" "2018-34" "2018-35" "2018-36" "2018-37" "2018-38"
[22] "2018-39" "2018-40" "2018-41" "2018-42" "2018-43" "2018-44" "2018-45"
[29] "2018-46" "2018-47" "2018-48" "2018-49" "2018-50" "2018-51" "2018-52"

请注意,修剪掉我们不想要的日期甚至使用文本日期字符串,因为所有日期都是固定的宽度字符串,并且如果需要,将保留为零填充。因此,排序因此与实际数字一样有效。


0
投票

这是使用str_pad包中的stringr函数的可能性:

weeks <- str_pad(41:65 %% 53 + 1, 2, "left", "0")
years <- ifelse(41:65 <= 52, "2018", "2019")
paste(years, weeks, sep = "-")
     [1] "2018-42" "2018-43" "2018-44" "2018-45" "2018-46" "2018-47" "2018-48" "2018-49" "2018-50" "2018-51" "2018-52" "2018-53" "2019-01" "2019-02" "2019-03" "2019-04" "2019-05" "2019-06" "2019-07" "2019-08" "2019-09"
[22] "2019-10" "2019-11" "2019-12" "2019-13"

正如我刚从其他两个答案中学到的那样,sprintfstr_pad提供了一个替代方案。所以你也可以使用

weeks <- sprintf("%02d", 41:65 %% 53 + 1)

0
投票

这是使用strftime的可能性:

weeks <- seq(from = ISOdate(2018,12,10), to = ISOdate(2019,4,1), by="week")
strftime(weeks,format="%Y-%W") 
© www.soinside.com 2019 - 2024. All rights reserved.