Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.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/8/design-patterns/2.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 “转动字符串”;2020年12月27日00:00:00“;约会?_R_Lubridate - Fatal编程技术网

R “转动字符串”;2020年12月27日00:00:00“;约会?

R “转动字符串”;2020年12月27日00:00:00“;约会?,r,lubridate,R,Lubridate,字符串:“12/27/2020 00:00:00” 我想把它格式化为日期。尝试: library(lubridate) parse_date_time('12/27/2020 00:00:00', mdy) Error in as.character(x) : cannot coerce type 'closure' to vector of type 'character' 然后尝试: mdy('12/27/2020 00:00:00') [1] NA Warning message:

字符串:
“12/27/2020 00:00:00”

我想把它格式化为日期。尝试:

library(lubridate)
parse_date_time('12/27/2020 00:00:00', mdy)
Error in as.character(x) : 
  cannot coerce type 'closure' to vector of type 'character'
然后尝试:

mdy('12/27/2020 00:00:00')
[1] NA
Warning message:
All formats failed to parse. No formats found. 
as_date('12/27/2020 00:00:00')
[1] NA
Warning message:
All formats failed to parse. No formats found. 
然后尝试:

mdy('12/27/2020 00:00:00')
[1] NA
Warning message:
All formats failed to parse. No formats found. 
as_date('12/27/2020 00:00:00')
[1] NA
Warning message:
All formats failed to parse. No formats found. 

如何将此字符串转换为日期?

我们可以在
as.date
中使用
格式

as.Date(str1, "%m/%d/%Y")
#[1] "2020-12-27"

或者,如果我们需要
parse\u date\u time
,请将
格式指定为字符串

parse_date_time('12/27/2020 00:00:00',  'mdy HMS')
#[1] "2020-12-27 UTC"
要转换为
Date
类,请使用
as.Date
as\u Date

as_date(parse_date_time('12/27/2020 00:00:00',  'mdy HMS'))
#[1] "2020-12-27"
library(anytime)
anydate('12/27/2020 00:00:00')
#[1] "2020-12-27"

对于
tidyverse
中的
格式
,它需要匹配完整格式。这里,我们还有小时:分钟:秒,所以我们需要
\u hms


或者,可以使用
anydate自动拾取此信息

as_date(parse_date_time('12/27/2020 00:00:00',  'mdy HMS'))
#[1] "2020-12-27"
library(anytime)
anydate('12/27/2020 00:00:00')
#[1] "2020-12-27"

谢谢将tidyverse格式与
mdy_hms
一起使用,是否可以只提取日期部分并将其格式化为日期?i、 e.放下hms?@DougFir在执行
解析日期时间
后,您可以使用
作为.date
作为日期