Using anytime() function in R

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

Using anytime() function in R

问题

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

  1. library(lubridate)
  2. x1 <- "03-01-2019 01:00:00"
  3. x2 <- "23-01-2019 17:00:00"
  4. anytime(x1)
  5. [1] "2019-03-01 01:00:00 CET"
  6. anytime(x2)
  7. [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.

  1. library(lubridate)
  2. x1&lt;-&quot;03-01-2019 01:00:00&quot;
  3. x2&lt;-&quot;23-01-2019 17:00:00&quot;
  4. anytime(x1)
  5. [1] &quot;2019-03-01 01:00:00 CET&quot;
  6. anytime(x2)
  7. [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:

  1. 你可以使用 `addFormats()` 来将特定格式添加到由 `anytime()`(和/或 `anydate()`)已知和使用的堆栈中。
  2. > library(anytime)
  3. > addFormats("%d-%m-%Y %H:%M:%S")
  4. > anytime(c("03-01-2019 01:00:00", "23-01-2019 17:00:00"))
  5. [1] "2019-01-03 01:00:00 CST" "2019-01-23 17:00:00 CST"
  6. >

关于这个网站之前已经解释过多次,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"

  1. 你可以在不使用 `head()` 的情况下使用它来查看所有格式。
  2. <details>
  3. <summary>英文:</summary>
  4. You can use `addFormats()` to add a specific format to the stack known and used by `anytime()` (and/or `anydate()`)
  5. &gt; library(anytime)
  6. &gt; addFormats(&quot;%d-%m-%Y %H:%M:%S&quot;)
  7. &gt; anytime(c(&quot;03-01-2019 01:00:00&quot;, &quot;23-01-2019 17:00:00&quot;))
  8. [1] &quot;2019-01-03 01:00:00 CST&quot; &quot;2019-01-23 17:00:00 CST&quot;
  9. &gt;
  10. As explained a few times before on this site, the `xx-yy` notation is ambiguous and interpreted differently in different parts of the world.
  11. 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:
  12. &gt; head(getFormats(), 12) ## abbreviated for display here
  13. [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;
  14. [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;
  15. [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;
  16. [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;
  17. &gt;
  18. You can use it without the `head()` to see all.
  19. </details>
  20. # 答案2
  21. **得分**: 2
  22. 使用`parsedate`包的另一种方法可能是:
  23. ```R
  24. library(parsedate)
  25. > parsedate::parse_date(x1)
  26. [1] "2019-03-01 01:00:00 UTC"
  27. > parsedate::parse_date(x2)
  28. [1] "2019-01-23 17:00:00 UTC"
英文:

An alternative approach could be using parsedate package:

  1. library(parsedate)
  2. &gt; parsedate::parse_date(x1)
  3. [1] &quot;2019-03-01 01:00:00 UTC&quot;
  4. &gt; parsedate::parse_date(x2)
  5. [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:

确定