简体   繁体   English

如何在R中将整数转换为日期格式?

[英]How to convert integer to date format in R?

I am trying to convert integer data from my data frame in R, to date format. 我正在尝试将R中的数据帧中的整数数据转换为日期格式。

The data is under column named svcg_cycle within orig_svcg_filtered data frame. 数据在orig_svcg_filtered数据帧内名为svcg_cycle的列下。

The original data looking something like 200502, 200503, and so forth, and I expect to turn it into yyyy-mm-dd format. 原始数据看起来像200502、200503等,我希望将其转换为yyyy-mm-dd格式。

I am trying to use this code: 我正在尝试使用以下代码:

as.Date(orig_svcg_filtered$svcg_cycle, origin = "2000-01-01")

but the output is not something that I expected: 但是输出不是我期望的:

[1] "2548-12-15" "2548-12-15" "2548-12-15" "2548-12-15" "2548-12-15" 

while it is supposed to be 2005-02-01, 2005-03-01, and so forth. 而应该是2005-02-01、2005-03-01等。

How to solve this? 如何解决呢?

If you have 如果你有

x <- c(200502, 200503)

Then 然后

as.Date(x, origin = "2000-01-01")

tells R you want the days 200,502 and 200,503 days after 2000-01-01. 告诉R您想要2000-01-01之后的200,502和200,503天。 From help("as.Date") : help("as.Date")

as.Date will accept numeric data (the number of days since an epoch), but only if origin is supplied. as.Date将接受数字数据(自一个纪元以来的天数),但仅在提供了origin的情况下。

So, integer data gives days after the origin supplied, not some sort of numeric code for the dates like 200502 for "2005-02-01". 因此,整数数据给出的是提供的原点后的天数,而不是日期的某种数字代码,例如“ 2005-02-01”的200502。

What you want is 你想要的是

as.Date(paste(substr(x, 1, 4), substr(x, 5, 6), "01", sep = "-"))

# [1] "2005-02-01" "2005-03-01"

The

paste(substr(x, 1, 4), substr(x, 5, 6), "01", sep = "-")

part takes your integers and creates strings like 部分接受您的整数并创建类似的字符串

# [1] "2005-02-01" "2005-03-01"

Then as.Date() knows how to deal with them. 然后as.Date()知道如何处理它们。

You could alternatively do something like 您可以选择做类似的事情

as.Date(paste0(x, "01"), format = "%Y%m%d")

# [1] "2005-02-01" "2005-03-01"

This just pastes on an "01" to each element (for the day), converts to character, and tells as.Date() what format to read the date into. 这只是在每个元素上(当天)粘贴"01" ,转换为字符,然后告诉as.Date()将日期读入的格式。 (See help("as.Date") and help("strptime") ). (请参阅help("as.Date")help("strptime") )。

I like to use Regex to fix these kinds of string formatting issues. 我喜欢使用Regex来解决这类字符串格式问题。 as.Date by default only checks for several standard date formats like YYYY-MM-DD. 默认情况下, as.Date仅检查几种标准日期格式,例如YYYY-MM-DD。 origin is used when you have an integer date (ie seconds from some reference point), but in this case your date is actually not an integer date, rather it's just a date formatted as a string of integers. 当您有一个整数日期(即距某个参考点的秒数)时,将使用origin ,但是在这种情况下,您的日期实际上不是整数日期,而只是一个格式化为整数字符串的日期。

We simply split the month and day with a dash, and add a day, in this case the first of the month, to make it a valid date (you must have a day to store it as a date object in R). 我们只需用破折号将月份和日期分开,然后添加一天(在这种情况下是月份的第一天)以使其成为有效日期(您必须有一天将其存储为R中的日期对象)。 The Regex bit captures the first 4 digits in group one and final two digits in group two. 正则表达式位捕获组1中的前4位和组2中的后两位。 We then combine the two groups, separated by dashes, along with the day. 然后,我们将用破折号分隔的两组与日期合并在一起。

as.Date(gsub("^(\\d{4})(\\d{2})", "\\1-\\2-01", x))

[1] "2005-02-01" "2005-03-01"

You don't need to specify format in this case, because YYYY-MM-DD is one of the standard formats as.Date checks, however, the format argument is format = "%Y-%m-%d" 在这种情况下,您无需指定format ,因为YYYY-MM-DD是as.Date检查的标准格式as.Date ,但是format参数为format = "%Y-%m-%d"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM