4

I have a date variable in a data frame with date in "YYYY-MM-DD" format.

I used the separate function(below) in tidyr package which worked but it does not add columns to the table.

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

How can I get "Year", "Month" & "Day" variables added to the end of the table?

UseR10085
  • 7,120
  • 3
  • 24
  • 54
aditya tandel
  • 67
  • 1
  • 1
  • 4

1 Answers1

7

You need to define

  • the data frame as first, and
  • the column with the date as second

argument to separate.

See this example:

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

Which yields:

  Year Month Day
1 2017    02  23
2 2017    02  22
setempler
  • 1,681
  • 12
  • 20