从R中的年、月和周数据插值年、月、日

从R中的年、月和周数据插值年、月、日,r,lubridate,R,Lubridate,我继承了一个数据集,其测量范围为1970年至2019年。头部和尾部看起来像这样: year month week X1 1970 1 1 0.21 1970 1 2 0.22 1970 1 3 0.34 1970 1 4 0.34 1970 2 5 0.35 1970 2 6 0.25 ... 2019 11 47 0.063 201

我继承了一个数据集,其测量范围为1970年至2019年。头部和尾部看起来像这样:

year  month  week    X1 
1970      1     1   0.21
1970      1     2   0.22
1970      1     3   0.34
1970      1     4   0.34
1970      2     5   0.35
1970      2     6   0.25
... 
2019     11    47   0.063
2019     12    48   0.062
2019     12    49   0.068
2019     12    50   0.067
2019     12    51   0.074
2019     12    52   0.075
as.POSIXct("1970-01-05") + lubridate::days(0:2616 * 7)

在每周第一天(即周一)记录X1的每次观察结果。我想用ISO8601格式(yyyy-mm-dd)创建一个日期列。给定年、月和周,应该可以提取每月哪一天是每周的星期一。注:测量是每周一进行的,不考虑节假日

这真的只是一句台词。自1970年1月5日起,您可以使用如下
lubridate
包生成每周一的向量:

year  month  week    X1 
1970      1     1   0.21
1970      1     2   0.22
1970      1     3   0.34
1970      1     4   0.34
1970      2     5   0.35
1970      2     6   0.25
... 
2019     11    47   0.063
2019     12    48   0.062
2019     12    49   0.068
2019     12    50   0.067
2019     12    51   0.074
2019     12    52   0.075
as.POSIXct("1970-01-05") + lubridate::days(0:2616 * 7)
直到今天

下面是一个reprex,展示了自1970年初以来的前100个星期一:

head(如POSIXct(“1970-01-05”)+lubridate::天(0:2616*7),100)
#>[1]“英国夏令时1970-01-05”“英国夏令时1970-01-12”“英国夏令时1970-01-19”“英国夏令时1970-01-26”
#>[5]“1970-02-02英国夏令时”“1970-02-09英国夏令时”“1970-02-16英国夏令时”“1970-02-23英国夏令时”
#>[9]“英国夏令时1970-03-02”“英国夏令时1970-03-09”“英国夏令时1970-03-16”“英国夏令时1970-03-23”
#>[13]“英国夏令时1970-03-30”“英国夏令时1970-04-06”“英国夏令时1970-04-13”“英国夏令时1970-04-20”
#>[17]“英国夏令时1970-04-27”“英国夏令时1970-05-04”“英国夏令时1970-05-11”“英国夏令时1970-05-18”
#>[21]“英国夏令时1970-05-25”“英国夏令时1970-06-01”“英国夏令时1970-06-08”“英国夏令时1970-06-15”
#>[25]“英国夏令时1970-06-22”“英国夏令时1970-06-29”“英国夏令时1970-07-06”“英国夏令时1970-07-13”
#>[29]“英国夏令时1970-07-20”“英国夏令时1970-07-27”“英国夏令时1970-08-03”“英国夏令时1970-08-10”
#>[33]“英国夏令时1970-08-17”“英国夏令时1970-08-24”“英国夏令时1970-08-31”“英国夏令时1970-09-07”
#>[37]“英国夏令时1970-09-14”“英国夏令时1970-09-21”“英国夏令时1970-09-28”“英国夏令时1970-10-05”
#>[41]“英国夏令时1970-10-12”“英国夏令时1970-10-19”“英国夏令时1970-10-26”“英国夏令时1970-11-02”
#>[45]“英国夏令时1970-11-09”“英国夏令时1970-11-16”“英国夏令时1970-11-23”“英国夏令时1970-11-30”
#>[49]“英国夏令时1970-12-07”“英国夏令时1970-12-14”“英国夏令时1970-12-21”“英国夏令时1970-12-28”
#>[53]“1971-01-04英国夏令时”“1971-01-11英国夏令时”“1971-01-18英国夏令时”“1971-01-25英国夏令时”
#>[57]“1971-02-01英国夏令时”“1971-02-08英国夏令时”“1971-02-15英国夏令时”“1971-02-22英国夏令时”
#>[61]“1971-03-01英国夏令时”“1971-03-08英国夏令时”“1971-03-15英国夏令时”“1971-03-22英国夏令时”
#>[65]“1971-03-29英国夏令时”“1971-04-05英国夏令时”“1971-04-12英国夏令时”“1971-04-19英国夏令时”
#>[69]“英国夏令时1971-04-26”“英国夏令时1971-05-03”“英国夏令时1971-05-10”“英国夏令时1971-05-17”
#>[73]“英国夏令时1971-05-24”“英国夏令时1971-05-31”“英国夏令时1971-06-07”“英国夏令时1971-06-14”
#>[77]“1971-06-21英国夏令时”“1971-06-28英国夏令时”“1971-07-05英国夏令时”“1971-07-12英国夏令时”
#>[81]“1971-07-19英国夏令时”“1971-07-26英国夏令时”“1971-08-02英国夏令时”“1971-08-09英国夏令时”
#>[85]“英国夏令时1971-08-16”“英国夏令时1971-08-23”“英国夏令时1971-08-30”“英国夏令时1971-09-06”
#>[89]“英国夏令时1971-09-13”“英国夏令时1971-09-20”“英国夏令时1971-09-27”“英国夏令时1971-10-04”
#>[93]“英国夏令时1971-10-11”“英国夏令时1971-10-18”“英国夏令时1971-10-25”“英国夏令时1971-11-01格林尼治标准时间”
#>[97]“1971-11-08 GMT”“1971-11-15 GMT”“1971-11-22 GMT”“1971-11-29 GMT”

由(v0.3.0)

使用
lubridate
软件包于2020-02-24创建,您可以按以下方式计算:


df这里有一个想法。注意,在本演示中,我只使用了示例中的前六行

library(dplyr)
library(lubridate)

date_seq <- tibble(
  # Create a data frame with dates from 1970 to 2019
  date = seq.Date(as.Date("1970-01-01"), as.Date("2019-12-31"), by = 1)
) %>%
  # Create weekday
  mutate(weekday = weekdays(date)) %>%
  # Filter for Monday
  filter(weekday %in% "Monday") %>%
  # Create year, month
  mutate(year = year(date), month = month(date)) %>%
  # Create week number
  mutate(week = 1:n()) %>%
  # Join the data
  left_join(dat, by = c("year", "month", "week"))
date_seq
# # A tibble: 2,609 x 6
#    date       weekday  year month  week    X1
#    <date>     <chr>   <dbl> <dbl> <int> <dbl>
#  1 1970-01-05 Monday   1970     1     1  0.21
#  2 1970-01-12 Monday   1970     1     2  0.22
#  3 1970-01-19 Monday   1970     1     3  0.34
#  4 1970-01-26 Monday   1970     1     4  0.34
#  5 1970-02-02 Monday   1970     2     5  0.35
#  6 1970-02-09 Monday   1970     2     6  0.25
#  7 1970-02-16 Monday   1970     2     7 NA   
#  8 1970-02-23 Monday   1970     2     8 NA   
#  9 1970-03-02 Monday   1970     3     9 NA   
# 10 1970-03-09 Monday   1970     3    10 NA   
# # ... with 2,599 more rows
库(dplyr)
图书馆(lubridate)
日期顺序%
#创建工作日
变异(工作日=工作日(日期))%>%
#星期一过滤
筛选器(工作日%中的%“星期一”)%%>%
#创建年、月
变异(年=年(日),月=月(日))%>%
#创建周数
变异(周=1:n())%>%
#加入数据
左联合(dat,by=c(“年”、“月”、“周”))
日期
##A tibble:2609 x 6
#日期工作日年月周X1
#               
#1970年01月05日星期一1970年11月21日
#1970年01月12日星期一1970年01月22日
#3 1970-01-19星期一1970 1 3 0.34
#4 1970-01-26 1970年1月4日星期一0.34
#5 1970-02-02星期一1970 2 5 0.35
#6 1970-02-09 1970年2月6日星期一0.25
#7 1970-02-16星期一1970年2月7日北美
#8 1970-02-23星期一1970年2月8日北美
#9 1970-03-02 1970年3月9日星期一北美
#10 1970-03-09 1970年3月10日星期一北美
# # ... 还有2599行
数据

dat <- read.table(text = "year  month  week    X1 
1970      1     1   0.21
1970      1     2   0.22
1970      1     3   0.34
1970      1     4   0.34
1970      2     5   0.35
1970      2     6   0.25",
                header = TRUE, stringsAsFactors = FALSE)
dat您可以使用base R:

df <- data.frame(
  year = c(1970,1970,1970,1970,1970,1970,2019,2019,2019,2019),
  month = c(1,1,1,1,2,2,11,12,12,12),
  week = c(1,2,3,4,5,6,47,48,49,50)
)

df$date_string <- paste(df$year,df$week,1, sep = "-")
df$date <- as.Date(x = df$date_string,format = "%Y-%U-%u")

df也许我遗漏了什么,但你也可以尝试
seq.Date(as.Date(“1970-01-05”)、as.Date(“2020-01-01”),by=“week”)