Using anytime() function in R

huangapple go评论51阅读模式
英文:

Using anytime() function in R

问题

我正在尝试使用anytime()函数在R中转换日期和时间的字符串。该字符串具有格式'日期-月份-年 小时-分钟-秒'。似乎anytime()函数对某些情况不起作用,如下面的示例所示。

library(lubridate)
x1 <- "03-01-2019 01:00:00"
x2 <- "23-01-2019 17:00:00"

anytime(x1)
[1] "2019-03-01 01:00:00 CET"
anytime(x2)
[1] NA

我正在努力找出如何解决这个问题。感谢您的帮助 Using anytime() function in R

英文:

I am trying to convert a string of date and time in R using anytime() function. The string have the format 'date-month-year hour-minute-second'. It seems that the anytime() function does not work for a few cases, as shown in the example below.

library(lubridate)
x1&lt;-&quot;03-01-2019 01:00:00&quot;
x2&lt;-&quot;23-01-2019 17:00:00&quot;

anytime(x1)
[1] &quot;2019-03-01 01:00:00 CET&quot;
anytime(x2)
[1] NA

I am trying to figure out how to get rid of this problem. Thanks for your help Using anytime() function in R

答案1

得分: 3

Sure, here is the translated code part:

你可以使用 `addFormats()` 来将特定格式添加到由 `anytime()`(和/或 `anydate()`)已知和使用的堆栈中。

> library(anytime)
> addFormats("%d-%m-%Y %H:%M:%S")
> anytime(c("03-01-2019 01:00:00", "23-01-2019 17:00:00"))
[1] "2019-01-03 01:00:00 CST" "2019-01-23 17:00:00 CST"
>

关于这个网站之前已经解释过多次,xx-yy 的表示法在世界各地有不同的解释方式,因此在不同地区会有不同的解释。

因此,anytime 根据用作分隔符的方式进行解释:在北美,/ 更常见,所以我们使用 "mm/dd/yyyy"。而在欧洲,破折号更常见,所以 "dd-mm-yyyy" 以这种方式开始。你可以使用 getFormats() 来查看 anytime() 中的格式。在一个新的会话中:

head(getFormats(), 12) ## 这里显示的是缩写
[1] "%Y-%m-%d %H:%M:%S%f" "%Y-%m-%e %H:%M:%S%f" "%Y-%m-%d %H%M%S%f"
[4] "%Y-%m-%e %H%M%S%f" "%Y/%m/%d %H:%M:%S%f" "%Y/%m/%e %H:%M:%S%f"
[7] "%Y%m%d %H%M%S%f" "%Y%m%d %H:%M:%S%f" "%m/%d/%Y %H:%M:%S%f"
[10] "%m/%e/%Y %H:%M:%S%f" "%m-%d-%Y %H:%M:%S%f" "%m-%e-%Y %H:%M:%S%f"


你可以在不使用 `head()` 的情况下使用它来查看所有格式。

<details>
<summary>英文:</summary>

You can use `addFormats()` to add a specific format to the stack known and used by `anytime()` (and/or `anydate()`)

    &gt; library(anytime)
    &gt; addFormats(&quot;%d-%m-%Y %H:%M:%S&quot;)
    &gt; anytime(c(&quot;03-01-2019 01:00:00&quot;, &quot;23-01-2019 17:00:00&quot;))
    [1] &quot;2019-01-03 01:00:00 CST&quot; &quot;2019-01-23 17:00:00 CST&quot;
    &gt; 

As explained a few times before on this site, the `xx-yy` notation is ambiguous and interpreted differently in different parts of the world. 

So `anytime` is guided by use as the separator: `/` is more common in North America so we use &quot;mm/dd/yyy&quot;. On the other hand a hyphen is more common in Europe so the &quot;dd-mm-yyyy&quot; starts that way.  You can use `getFormats()` to see the formats in `anytime()`.  In a fresh session:

    &gt; head(getFormats(), 12)   ## abbreviated for display here
     [1] &quot;%Y-%m-%d %H:%M:%S%f&quot; &quot;%Y-%m-%e %H:%M:%S%f&quot; &quot;%Y-%m-%d %H%M%S%f&quot;  
     [4] &quot;%Y-%m-%e %H%M%S%f&quot;   &quot;%Y/%m/%d %H:%M:%S%f&quot; &quot;%Y/%m/%e %H:%M:%S%f&quot;
     [7] &quot;%Y%m%d %H%M%S%f&quot;     &quot;%Y%m%d %H:%M:%S%f&quot;   &quot;%m/%d/%Y %H:%M:%S%f&quot;
    [10] &quot;%m/%e/%Y %H:%M:%S%f&quot; &quot;%m-%d-%Y %H:%M:%S%f&quot; &quot;%m-%e-%Y %H:%M:%S%f&quot;
    &gt; 

You can use it without the `head()` to see all.

</details>



# 答案2
**得分**: 2

使用`parsedate`包的另一种方法可能是: 

```R
library(parsedate)
> parsedate::parse_date(x1)
[1] "2019-03-01 01:00:00 UTC"
> parsedate::parse_date(x2)
[1] "2019-01-23 17:00:00 UTC"
英文:

An alternative approach could be using parsedate package:

library(parsedate)
&gt; parsedate::parse_date(x1)
[1] &quot;2019-03-01 01:00:00 UTC&quot;
&gt; parsedate::parse_date(x2)
[1] &quot;2019-01-23 17:00:00 UTC&quot;

huangapple
  • 本文由 发表于 2023年2月19日 06:35:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定