英文:
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
我正在努力找出如何解决这个问题。感谢您的帮助
英文:
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<-"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
I am trying to figure out how to get rid of this problem. Thanks for your help
答案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()`)
> 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"
>
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 "mm/dd/yyy". On the other hand a hyphen is more common in Europe so the "dd-mm-yyyy" starts that way. You can use `getFormats()` to see the formats in `anytime()`. In a fresh session:
> head(getFormats(), 12) ## abbreviated for display here
[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"
>
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)
> parsedate::parse_date(x1)
[1] "2019-03-01 01:00:00 UTC"
> parsedate::parse_date(x2)
[1] "2019-01-23 17:00:00 UTC"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论