[英]convert year and month into date format
I have dates in format我有日期格式
192607 192608 192607 192608
and want to transform them so that they are in the following format and can be used for a xts object并希望将它们转换为以下格式并且可用于 xts object
1926-07-01 1926-08-01 1926-07-01 1926-08-01
I have tried working with as.date and paste() but couldn't make it work.我曾尝试使用 as.date 和 paste() 但无法使其工作。 Help is very much appreciated.
非常感谢您的帮助。 Thank you!!
谢谢!!
You need to paste then put format date.您需要粘贴然后输入格式日期。 Something like this:
像这样的东西:
dates <- c("192607", "192608")
dates <- paste0(dates,"01")
dates <- as.Date(dates, format ="%Y%m%d")
dates
The result is结果是
[1] "1926-07-01" "1926-08-01"
Assuming all the dates will be converted to the first of the month, this lubridate
solution works.假设所有日期都将转换为该月的第一天,则此
lubridate
解决方案有效。
library(lubridate)
dates <- c(192607, 192608)
dates <- paste0(dates, '01') # add 01 for day of month
# output: "19260701" "19260801"
dates <- ymd(dates)
# output: "1926-07-01" "1926-08-01"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.