如何在r中分隔年月日日期

How to separate date in year, month and day in r

我在数据框中有一个日期变量,日期格式为“YYYY-MM-DD”。

我在 tidyr 包中使用了单独的函数(如下),但它没有向 table 添加列。

separate(<table name>, "<date variable>", c("Year", "Month", "Day"), sep = "-")

如何将“年”、“月”和“日”变量添加到 table 的末尾?

你需要定义

  • 首先是数据框,
  • 第二个日期的列

separate 的参数。

看这个例子:

d <- data.frame(date = c("2017-02-23", "2017-02-22"))
separate(d, "date", c("Year", "Month", "Day"), sep = "-")

产生:

  Year Month Day
1 2017    02  23
2 2017    02  22