简体   繁体   English

如何在R中将字符串转换为日期格式

[英]How to convert string to date format in R

I have a column of strings in the following format: 我有一列格式如下的字符串:

Wed, 6 Dec 2000 08:47:00 -0800 (PST)

How can I convert this into date format using lubridate or another package? 如何使用lubridate或其他软件包将其转换为日期格式? I have done this before, but there was no -0800 (PST) at the end. 我之前已经做过,但是最后没有-0800 (PST)

Thank you. 谢谢。

I was able to get a result using strptime() without even worrying about the timezone name at the end: 我可以使用strptime()获得结果,而不必担心最后的时区名称:

> x - "Wed, 6 Dec 2000 08:47:00 -0800 (PST)"
> strptime(x,  "%a, %d %b %Y %H:%M:%S %z")
[1] "2000-12-07 00:47:00"

However, if you want to remove the timezone name, you can use substr() to do this: 但是,如果要删除时区名称,可以使用substr()来执行此操作:

> strptime(substr(x, 1, nchar(x)-6),  "%a, %d %b %Y %H:%M:%S %z")
[1] "2000-12-07 00:47:00"

We can also use parse_date_time 我们也可以使用parse_date_time

library(lubridate)
parse_date_time(x, "adbY HMS z", tz = "US/Pacific")
#[1] "2000-12-06 08:47:00 PST"

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

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