Reputation: 119
I have a column with date formatted as MM-DD-YYYY, in the Date format. I want to add 2 columns one which only contains YYYY and the other only contains MM.
How do I do this?
Upvotes: 2
Views: 15827
Reputation: 887851
A base R
option would be to remove the substring with sub
and then read with read.table
df1[c('month', 'year')] <- read.table(text=sub("-\\d{2}-", ",", df1$date), sep=",")
Or using tidyverse
library(tidyverse)
separate(df1, date, into = c('month', 'day', 'year') %>%
select(-day)
Note: it may be better to convert to datetime class instead of using the string formatting.
df1 %>%
mutate(date =mdy(date), month = month(date), year = year(date))
df1 <- data.frame(date = c("05-21-2017", "06-25-2015"))
Upvotes: 1
Reputation: 368539
Once again base R gives you all you need, and you should not do this with sub-strings.
Here we first create a data.frame
with a proper Date
column. If your date is in text format, parse it first with as.Date()
or my anytime::anydate()
(which does not need formats).
Then given the date creating year and month is simple:
R> df <- data.frame(date=Sys.Date()+seq(1,by=30,len=10))
R> df[, "year"] <- format(df[,"date"], "%Y")
R> df[, "month"] <- format(df[,"date"], "%m")
R> df
date year month
1 2017-12-29 2017 12
2 2018-01-28 2018 01
3 2018-02-27 2018 02
4 2018-03-29 2018 03
5 2018-04-28 2018 04
6 2018-05-28 2018 05
7 2018-06-27 2018 06
8 2018-07-27 2018 07
9 2018-08-26 2018 08
10 2018-09-25 2018 09
R>
If you want year or month as integers, you can wrap as as.integer()
around the format.
Upvotes: 11