【问题标题】:How to add a separate column to data frame with lubridate? Would like to have numerical month and word month in the data frame?如何使用 lubridate 向数据框添加单独的列?想在数据框中有数字月份和单词月份吗?
【发布时间】:2018-10-09 23:15:07
【问题描述】:

lubridate 允许我们将 y-m-d 格式分解为月、年周等...我已经用我的数据集完成了这个。我有数字月份的月份,但想要一个带有月份缩写的单独列。我可以转换它们,但我想在数据框中同时包含数字和单词月份。除了手动添加列向量之外,还有其他方法吗?

【问题讨论】:

    标签: r dataframe lubridate


    【解决方案1】:

    lubridate::month 生成数字月份。添加参数label = TRUE 生成月份缩写。您可以使用dplyr::mutate 添加新列。

    例如:

    library(dplyr)
    library(lubridate)
    
    data.frame(Date = as_date("2001-10-11")) %>% 
      mutate(Month = month(Date), 
             MonthAbb = month(Date, label = TRUE))
    
            Date Month MonthAbb
    1 2001-10-11    10      Oct
    

    【讨论】:

    • 好!如果解决了问题,请采纳答案。
    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2022-12-13
    • 2020-11-21
    • 2020-12-03
    • 1970-01-01
    • 2020-05-20
    • 1970-01-01
    相关资源
    最近更新 更多