0

I'm new to rstudio and so this might have a very simple answer. I have imported a table with 12000+ rows. One of my columns has different dates in a "year-month-day" format and I want to split this one column into three separate year, month, and day columns in order to do some analysis using only the months. I've seen numerous answers to this question but none seem to be working for me. How can I convert so many rows into separate columns without writing each date out separately.

It may be important to note that the column I am trying to convert is in date formate as "Y%-m%-%d".

The table I have imported looks like this:

        date_observed  latitude   Range
1       2018-03-04     9.934524   "6 to 15"
2       2019-06-20     9.942766   "6 to 15"
3       2018-07-07     14.007714  "6 to 15"
...
12408   2019-06-05     70.67493   "66 to 75"

I want it to look like this:

        year  month  day  latitude   Range
1       2018  03     04   9.934524   "6 to 15"
2       2019  06     20   9.942766   "6 to 15"
3       2018  07     07   14.007714  "6 to 15"
...
12408   2019  06     05   70.67493   "66 to 75"
  • It is easier to help if you share some information about your data. You can share few lines of data using `dput(head(df))` where `df` is the name of your dataframe. – Ronak Shah Apr 15 '20 at 02:36

1 Answers1

3

There are multiple ways to do this. If your data is like this :

df <- data.frame(dates = c('2019-01-03', '2020-03-21'))

1) In base R,we can change the dates to date class and extract day, month and year respectively using format.

df$dates <- as.Date(df$dates)
transform(df, date = format(dates, "%d"), 
               month = format(dates, "%m"), year = format(dates, "%Y"))

#       dates date month year
#1 2019-01-03   03    01 2019
#2 2020-03-21   21    03 2020

2) Using tidyr::separate we split the string on "-"

tidyr::separate(df, dates, c('year', 'month', 'day'), sep = "-",remove = FALSE)

3) Same as 1) but using dplyr + lubridate

library(dplyr)
library(lubridate)

df %>%
  mutate(dates = as.Date(dates), 
         date = day(dates), month = month(dates), year = year(dates))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213