r - 将 year.week 转换为 date
问题描述
我有 YYYY.week 格式的时间:
x <- c("2016.49", "2016.50", "2016.51", "2016.52", "2017.01", "2017.02", "2017.03", "2017.04", "2017.05")
有没有办法像这样创建将其转换为日期变量(日期不正确,但这是所需的格式):
as.Date(x[1])
"2017-02-01"
解决方案
一种lubridate
选择可能是:
ymd(paste0(substr(x, 1, 4), "-01", "-01"))+weeks(as.numeric(substr(x, nchar(x)-1, nchar(x)))-1)
[1] "2016-12-02" "2016-12-09" "2016-12-16" "2016-12-23" "2017-01-01"
样本数据:
x <- c("2016.49", "2016.50", "2016.51", "2016.52", "2017.01")
推荐阅读
- javascript - 查找匹配条件的数组索引,然后推入另一个数组
- linux - 反向函数通过shell读取应用csv中的特定列
- laravel - Laravel Cache,正在重新查询缓存的查询
- vba - VBA:循环遍历各种 Excel 文件并将 cokumn 复制到主文件中
- django - 如何创建连接到现有 MySQL 表的 Django REST Framework API,而不是通过 models.py 创建它们
- ssl - erlang elixir nif char * 数据到二进制文件的 OCSP 请求失败
- java - java.lang.NoClassDefFoundError:解析失败:Lcom/google/android/gms/common/internal/zzbo;
- scala - 在Scala中无条件返回字符串
- google-app-engine - 无法访问受版本控制的 Google App Engine 网站
- angular6 - 为什么我在 `http.get().pipe()` 上得到“未定义”