Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to separate date in year, month and day in r [duplicate]

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?

like image 583
aditya tandel Avatar asked Mar 10 '23 19:03

aditya tandel


1 Answers

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
like image 79
setempler Avatar answered Mar 12 '23 12:03

setempler