r - 在 R 中分割日期

标签 r dataframe date

我的数据框的时间列如下所示

# time
# %Y-%m-%d
# 2000-01-01
# 2000-01-02
# ...

我想将日期列分成一个代表年份和一个代表月份(我不需要日期) 因此输出应如下所示:

# year  month
# %Y    %m
# 2000  01
# 2000  02
# ...

最佳答案

lubridate 包中,使用 year()month() 函数分别从日期对象中提取它们。

library(data.table)
library(lubridate)

DT <- as.data.table(yourDataFrame)

DT[, Date := ymd(time)]
DT[, year := year(Date)]
DT[, month := month(Date)]

关于r - 在 R 中分割日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61480697/

相关文章:

r - dplyr 工作流中带有 do() 的 broom::tidy() 因 summaryDefault 对象而失败

r - 将数据帧中的列乘以向量

r - 有没有办法将多列中的值转为列名?

c# - mysql 和 c# - 将日期与今天进行比较(仅当天)

r - ggplot2 - 在堆叠条形图中更改 `geom_rect` 颜色

r - 使用 4000 条记录计算 Moran's I

python - 比较两列中的两个数据帧并获取差异

python - 如何从基于行的字典列表创建 Pandas DataFrame

sql - 在插入时向日期时间添加分钟 - mysql

r - 如何将日期和时间从字符转换为日期时间类型