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