【问题标题】:why does date() not work after loading lubridate package?为什么加载 lubridate 包后 date() 不起作用?
【发布时间】:2017-05-18 15:41:23
【问题描述】:

每天,我使用 R 中的 date() 函数来创建一个以今天日期命名的新工作文件夹。但是,一旦我加载了 lubridate 包,它就不起作用了。当我删除包时 date() 再次工作。 lubridate 有什么作用,所以“日期”功能的工作方式不同?加载 lubridate 包时,消息是:

    Attaching package: ‘lubridate’. The following object is masked from 
    ‘package:base’: date. 

要获取今天的日期和时间,date(x) 中 x 的值应该是多少?

date() #works well
library(lubridate) 
date() #does not work now. Error Msg: Error in as.POSIXlt(x, tz = tz(x)) : 
    #argument "x" is missing, with no default
detach("package:lubridate", unload=TRUE)
date() #now it works again without "x"

【问题讨论】:

  • 使用base::date(),当函数在不同包中具有相同名称时,这是一个屏蔽问题。一种选择是使用::

标签: r date lubridate


【解决方案1】:

基本上 lubridate 包含一个名为“date”的函数,因此当您加载 lubridate 包时,您使用的是来自该包而不是基础包的 date() 函数。

如果您想使用 lubridate 包中的特定功能,只需键入 lubridate::"the name of the function goes here" 而无需加载 lubridate 包。

【讨论】:

    【解决方案2】:

    函数 date() 被同名函数 lubridate::date() 屏蔽

    一种解决方法是使用base::date()

    【讨论】:

      猜你喜欢
      • 2022-08-06
      • 2023-04-03
      • 2015-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-10
      • 1970-01-01
      相关资源
      最近更新 更多