英文:
How to convert a character into a date class?
问题
考虑以下数据
my_date <- "October 15, 2018"
我想将其转换为日期。
因此,我尝试使用as.Date
函数如下
as.Date(my_date, format = "%B %d, %Y")
但是,这只返回一个NA
,我无法理解问题出在哪里。
我该如何解决这个问题?
英文:
Consider the following data
my_date <- "October 15, 2018"
I want to convert it into a date.
Thus, I tried to use the as.Date
function like below
as.Date(my_date, format = "%B %d, %Y")
But, this returns just a NA
and I cannot understand what is the problem.
How can I fix this problem?
答案1
得分: 2
根据您的使用情况,您可能会选择是否要更改语言环境。如果后者适用,lubridate
解析器允许您仅针对该函数设置 locale
,而不更改您的环境:
lubridate::mdy("October 15, 2018", locale = "C")
#> [1] "2018-10-15"
或者如果它是数据导入的一部分,也许考虑使用 readr
,它在非美国系统上处理默认语言环境非常方便:
library(readr)
# 系统语言环境:
Sys.getlocale()
#> [1] "LC_COLLATE=Estonian_Estonia.utf8;LC_CTYPE=Estonian_Estonia.utf8;LC_MONETARY=Estonian_Estonia.utf8;LC_NUMERIC=C;LC_TIME=Estonian_Estonia.utf8"
# 例如,对于“%B %d, %Y”格式,我们需要“juuli”而不是“July”:
format(Sys.time(), "%B %d, %Y")
#> [1] "juuli 24, 2023"
# readr::default_locale(),将被 readr::parse_date() 使用:
default_locale()
#> <locale>
#> Numbers: 123,456.78
#> Formats: %AD / %AT
#> Timezone: UTC
#> Encoding: UTF-8
#> <date_names>
#> Days: Sunday (Sun), Monday (Mon), Tuesday (Tue), Wednesday (Wed), Thursday
#> (Thu), Friday (Fri), Saturday (Sat)
#> Months: January (Jan), February (Feb), March (Mar), April (Apr), May (May),
#> June (Jun), July (Jul), August (Aug), September (Sep), October
#> (Oct), November (Nov), December (Dec)
#> AM/PM: AM/PM
# 导入使用特定系统语言环境的外国日期格式的 CSV,而无需更改任何语言环境设置:
dummy_csv <- '"October 15, 2018"';
read_csv(I(dummy_csv), col_names = FALSE, col_types = cols(col_date(format = "%B %d, %Y")))
#> # A tibble: 1 × 1
#> X1
#> <date>
#> 1 2018-10-15
英文:
Depending on your use case you may or may not want to alter your locales. If the latter applies, lubridate
parsers allow you to set locale
just for the function, without changing your environment:
lubridate::mdy("October 15, 2018", locale = "C")
#> [1] "2018-10-15"
Or if it's part of data import, perhaps consider readr
, its default locale handling is quite convenient on non-US systems:
library(readr)
# system lcoale:
Sys.getlocale()
#> [1] "LC_COLLATE=Estonian_Estonia.utf8;LC_CTYPE=Estonian_Estonia.utf8;LC_MONETARY=Estonian_Estonia.utf8;LC_NUMERIC=C;LC_TIME=Estonian_Estonia.utf8"
# E.g. for "%B %d, %Y" to work we would need "juuli" instead of "July":
format(Sys.time(), "%B %d, %Y")
#> [1] "juuli 24, 2023"
# readr::default_locale(), will be used by readr::parse_date() :
default_locale()
#> <locale>
#> Numbers: 123,456.78
#> Formats: %AD / %AT
#> Timezone: UTC
#> Encoding: UTF-8
#> <date_names>
#> Days: Sunday (Sun), Monday (Mon), Tuesday (Tue), Wednesday (Wed), Thursday
#> (Thu), Friday (Fri), Saturday (Sat)
#> Months: January (Jan), February (Feb), March (Mar), April (Apr), May (May),
#> June (Jun), July (Jul), August (Aug), September (Sep), October
#> (Oct), November (Nov), December (Dec)
#> AM/PM: AM/PM
# import CSV that uses foreign date format for that specific system locale,
# without changing any locale settings:
dummy_csv <- '"October 15, 2018"';
read_csv(I(dummy_csv), col_names = FALSE, col_types = cols(col_date(format = "%B %d, %Y")))
#> # A tibble: 1 × 1
#> X1
#> <date>
#> 1 2018-10-15
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论