R strftime()行为 [英] R strftime() behavior
问题描述
以下内容不起作用:
DateTime2 = strftime(08/13/2010 05:26:24.350,format =%m /%d /%Y%H:%M:%OS,tz =GMT)
返回以下错误:
as.POSIXlt.character(x,tz = tz)中的错误:
字符串不是标准的明确格式
但以下工作:
DateTime2 = strftime(08/02/2010 06:50:29.450,格式=%m /%d /%Y%H:%M:%OS,tz =GMT)
第二行按照预期存储 DateTime2
任何想法?
使用 strftime
会发生什么?这里是 strftime
代码:
> strftime
函数(x,format =,tz =,usetz = FALSE,...)
格式(as.POSIXlt(x,tz = tz),format = format,usetz = usetz,
...)
< bytecode:0xb9a548c>
< environment:namespace:base>
它调用 as.POSIXlt
和THEN格式使用参数格式
。
如果您直接在您的示例中调用 as.POSIXlt
,而不给出参数格式
,则会发生什么:
> as.POSIXlt(08/13/2010 05:26:24.350,tz =GMT)
as.POSIXlt.character中的错误(08/13/2010 05:26:24.350,tz = GMT):
字符串不是标准的明确格式
原因是 as.POSIXlt
的代码如下:
> as.POSIXlt.character
function(x,tz =,format,...)
{
x < - unclass(x)
if(!missing(format ){
res < - strptime(x,format,tz = tz)
if(nzchar(tz))
attr(res,tzone)< - tz
return(res)
}
xx< - x [!is.na(x)]
if(!length(xx)){
res < strptime(x,%Y /%m /%d)
if(nzchar(tz))
attr(res,tzone)< - tz
return
}
else if(all(!is.na(strptime(xx,f< - %Y-%m-%d%H:%M:%OS,
tz = tz)))|| all(!is.na(strptime(xx,f< - %Y /%m /%d%H:%M:%OS,
tz = tz) ))|| all(!is.na(strptime(xx,f< - %Y-%m-%d%H:%M,
tz = tz))|| all(! is.na(strptime(xx,f < - %Y /%m /%d%H:%M,
tz = tz))|| all(!is.na(strptime ,f( - )%Y-%m-%d,
tz = tz)))|| all(!is.na(strptime(xx,f< %d,
tz = tz)))){
res < - strptime(x,f,tz = tz)
if(nzchar(tz))
attr(res,tzone)< - tz
return(res)
}
停止(字符串不是标准的明确格式)
}
< bytecode:0xb9a4ff0>
< environment:namespace:base>
如果没有提供格式
一个通用格式的系列,如果没有一个工作它会引发您的错误。
所有这些的原因是 strftime
(与 strptime
相反,因此混淆)不用于将字符转换为POSIXlt对象,而是将POSIXlt对象转换为字符。从 strftime
的帮助页面:
价值
格式方法和strftime返回代表
的字符向量。
要做什么您想直接使用 as.POSIXlt
,如下所示:
> as.POSIXlt(08/13/2010 05:26:24.350,tz =GMT,format =%m /%d /%Y%H:%M:%OS)
[1 ]2010-08-13 05:26:24 GMT
编辑: strong> FYI你的第二行代码也无效:
strftime(08/02/2010 06: 50:29.450,格式=%m /%d /%Y%H:%M:%OS,tz =GMT)
[1]02/20/0008 00:00:00
Below are listed two lines of code. Both are identical expect for the day and time but only one works. I am using R 3.1.
The following doesn't work:
DateTime2=strftime("08/13/2010 05:26:24.350", format="%m/%d/%Y %H:%M:%OS", tz="GMT")
Returns the following error:
Error in as.POSIXlt.character(x, tz = tz) :
character string is not in a standard unambiguous format
But the following works:
DateTime2=strftime("08/02/2010 06:50:29.450", format="%m/%d/%Y %H:%M:%OS", tz="GMT")
The second line stores DateTime2
as expected.
Any thoughts?
What happens when you use strftime
? Here is strftime
code:
> strftime
function (x, format = "", tz = "", usetz = FALSE, ...)
format(as.POSIXlt(x, tz = tz), format = format, usetz = usetz,
...)
<bytecode: 0xb9a548c>
<environment: namespace:base>
It calls as.POSIXlt
and THEN format using the argument format
.
If you call directly as.POSIXlt
on your example without giving an argument format
, here is what happens:
> as.POSIXlt("08/13/2010 05:26:24.350", tz="GMT")
Error in as.POSIXlt.character("08/13/2010 05:26:24.350", tz = "GMT") :
character string is not in a standard unambiguous format
The reason being that the code for as.POSIXlt
is the following:
> as.POSIXlt.character
function (x, tz = "", format, ...)
{
x <- unclass(x)
if (!missing(format)) {
res <- strptime(x, format, tz = tz)
if (nzchar(tz))
attr(res, "tzone") <- tz
return(res)
}
xx <- x[!is.na(x)]
if (!length(xx)) {
res <- strptime(x, "%Y/%m/%d")
if (nzchar(tz))
attr(res, "tzone") <- tz
return(res)
}
else if (all(!is.na(strptime(xx, f <- "%Y-%m-%d %H:%M:%OS",
tz = tz))) || all(!is.na(strptime(xx, f <- "%Y/%m/%d %H:%M:%OS",
tz = tz))) || all(!is.na(strptime(xx, f <- "%Y-%m-%d %H:%M",
tz = tz))) || all(!is.na(strptime(xx, f <- "%Y/%m/%d %H:%M",
tz = tz))) || all(!is.na(strptime(xx, f <- "%Y-%m-%d",
tz = tz))) || all(!is.na(strptime(xx, f <- "%Y/%m/%d",
tz = tz)))) {
res <- strptime(x, f, tz = tz)
if (nzchar(tz))
attr(res, "tzone") <- tz
return(res)
}
stop("character string is not in a standard unambiguous format")
}
<bytecode: 0xb9a4ff0>
<environment: namespace:base>
If no format
is given, it tries a serie of common format, if none of them work it raises the error you got.
The reason for all that is that strftime
(contrary to strptime
hence the confusion) is not used to convert a character into a POSIXlt object but to turn a POSIXlt object into a character. From the help page for strftime
:
Value
The format methods and strftime return character vectors representing the time.
To do what you wanted to do, use as.POSIXlt
directly as follows:
> as.POSIXlt("08/13/2010 05:26:24.350", tz="GMT", format="%m/%d/%Y %H:%M:%OS")
[1] "2010-08-13 05:26:24 GMT"
Edit: FYI you're second line of code didn't work either:
strftime("08/02/2010 06:50:29.450", format="%m/%d/%Y %H:%M:%OS", tz="GMT")
[1] "02/20/0008 00:00:00"
这篇关于R strftime()行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!