如何将一个字符转换为日期类?

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

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(&quot;October 15, 2018&quot;, locale = &quot;C&quot;)
#&gt; [1] &quot;2018-10-15&quot;

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()
#&gt; [1] &quot;LC_COLLATE=Estonian_Estonia.utf8;LC_CTYPE=Estonian_Estonia.utf8;LC_MONETARY=Estonian_Estonia.utf8;LC_NUMERIC=C;LC_TIME=Estonian_Estonia.utf8&quot;

# E.g. for &quot;%B %d, %Y&quot; to work we would need &quot;juuli&quot; instead of &quot;July&quot;:
format(Sys.time(), &quot;%B %d, %Y&quot;)
#&gt; [1] &quot;juuli 24, 2023&quot;

# readr::default_locale(), will be used by readr::parse_date() :
default_locale()
#&gt; &lt;locale&gt;
#&gt; Numbers:  123,456.78
#&gt; Formats:  %AD / %AT
#&gt; Timezone: UTC
#&gt; Encoding: UTF-8
#&gt; &lt;date_names&gt;
#&gt; Days:   Sunday (Sun), Monday (Mon), Tuesday (Tue), Wednesday (Wed), Thursday
#&gt;         (Thu), Friday (Fri), Saturday (Sat)
#&gt; Months: January (Jan), February (Feb), March (Mar), April (Apr), May (May),
#&gt;         June (Jun), July (Jul), August (Aug), September (Sep), October
#&gt;         (Oct), November (Nov), December (Dec)
#&gt; AM/PM:  AM/PM

# import CSV that uses foreign date format for that specific system locale, 
# without changing any locale settings:

dummy_csv &lt;- &#39;&quot;October 15, 2018&quot;&#39;;
read_csv(I(dummy_csv), col_names = FALSE, col_types = cols(col_date(format = &quot;%B %d, %Y&quot;)))
#&gt; # A tibble: 1 &#215; 1
#&gt;   X1        
#&gt;   &lt;date&gt;    
#&gt; 1 2018-10-15

huangapple
  • 本文由 发表于 2023年7月24日 18:35:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753633.html
匿名

发表评论

匿名网友

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

确定