转换为日期时间
cov <- c("2020-01", "2020-02", "2020-03", "2020-04", "2020-05", "2020-06",
"2020-07", "2020-08", "2020-09", "2020-10")
strptime(paste0(cov,"-1"),"%Y-%W-%w")
[1] "2020-01-06 CET" "2020-01-13 CET" "2020-01-20 CET" "2020-01-27 CET" "2020-02-03 CET"
[6] "2020-02-10 CET" "2020-02-17 CET" "2020-02-24 CET" "2020-03-02 CET" "2020-03-09 CET"
要获得您想要的结果,您只需要再次调用格式化
format(strptime(paste0(cov,"-1"),"%Y-%W-%w"),"%Y-%b")
[1] "2020-jan." "2020-jan." "2020-jan." "2020-jan." "2020-feb." "2020-feb." "2020-feb."
[8] "2020-feb." "2020-mar." "2020-mar."
注意:我使用了 %W 和 %w
%w
Weekday as decimal number (0–6, Sunday is 0).
%W
Week of the year as decimal number (00–53) using Monday as the first day of week (and typically with the first Monday of the year as day 1 of week 1). The UK convention.