Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将字符转换为日期时间_R - Fatal编程技术网

R 将字符转换为日期时间

R 将字符转换为日期时间,r,R,在data.frame中,我有一个日期时间戳,格式如下: head(x$time) [1] "Thu Oct 11 22:18:02 2012" "Thu Oct 11 22:50:15 2012" "Thu Oct 11 22:54:17 2012" [4] "Thu Oct 11 22:43:13 2012" "Thu Oct 11 22:41:18 2012" "Thu Oct 11 22:15:19 2012" 每次我试图用as.Date、lubridate或zoo将其转换时,都会出现

在data.frame中,我有一个日期时间戳,格式如下:

head(x$time)
[1] "Thu Oct 11 22:18:02 2012" "Thu Oct 11 22:50:15 2012" "Thu Oct 11 22:54:17 2012"
[4] "Thu Oct 11 22:43:13 2012" "Thu Oct 11 22:41:18 2012" "Thu Oct 11 22:15:19 2012"
每次我试图用
as.Date
lubridatezoo将其转换时,都会出现
NA
s或错误

如何将这次转换为可读形式

我试过:

 Time<-strptime(x$time,format="&m/%d/%Y  %H:$M")
    x$minute<-parse_date_time(x$time)
    x$minute<-mdy(x$time)
    x$minute<-as.Date(x$time,"%m/%d/%Y %H:%M:%S")
    x$minute<-as.time(x$time)
    x$minute<-as.POSIXct(x$time,format="%H:%M")
    x$minute<-minute(x$time)

Time您真正想要的是
strtime()
。尝试以下方法:

strptime(x$time, "%a %b %d %H:%M:%S %Y")

作为一个有趣的例子,你可以使用<代码> STRPIMTIME()/<代码>,考虑以下内容:

thedate <- "I came to your house at 11:45 on January 21, 2012."
strptime(thedate, "I came to your house at %H:%M on %B %d, %Y.")
# [1] "2012-01-21 11:45:00"

date另一个选项是使用
lubridate::parse_date_time()

或者更简单地说:

parse_date_time(x$time, "abdHMSY")
从文档中:

它在两个方面与base::strtime()不同。首先,它允许指定格式出现的顺序,而不需要包含分隔符和%前缀。这样一个格式化参数被称为“顺序”。其次,它允许用户指定多个格式顺序来处理异构的日期-时间字符表示


文档包含lubridate识别的所有格式(“abdHMSY”等)。

您能分享一些您尝试过的步骤吗?另外,请注意,
as.date
as.date
不同。您尝试了什么?查看
strtime
,了解
format
参数提供给
as.Date
和其他参数。
parse_date_time(x$time, "abdHMSY")