【问题标题】:R function to get only columns with datetimeR函数仅获取具有日期时间的列
【发布时间】:2020-12-29 14:18:15
【问题描述】:

我试图只获取日期时间格式的列。对数字使用 select_if 是有效的,但我找不到日期时间的函数:

只有数字列的结果集

orders %>% select_if(is.numeric) %>% View()

只有 dttm 列的结果集?

orders %>% select_if(is.datetime) %>% View()

提前致谢

【问题讨论】:

    标签: r dplyr lubridate


    【解决方案1】:

    您可以使用lubridate 中的is.POSIXtis.POSIXct 来选择日期时间列。

    library(dplyr)
    library(lubridate)
    
    orders %>% select_if(is.POSIXct)
    

    如果您使用的是dplyr 1.0.0 或更高版本。

    orders %>% select(where(is.POSIXct))
    

    【讨论】:

      【解决方案2】:

      base R中,我们可以使用Filter

      Filter(function(x) inherits(x, "POSIXct"), orders)
      

      【讨论】:

        猜你喜欢
        • 2013-10-06
        • 2018-08-05
        • 1970-01-01
        • 2013-11-29
        • 1970-01-01
        • 2023-03-11
        • 2015-06-23
        • 2017-08-25
        • 1970-01-01
        相关资源
        最近更新 更多