I got the data from someone which has following types of dates:
"Jan-20", "Feb-19", "May-18"
I want to convert them into following format:
"01-20", "02-19", "05-18"
But whenever I try to convert the column to as.date
it returns NA values.
eg
df<- data.frame(MonthYear = c("Jan-20", "Feb-19", "May-18"))
df$MonthYear<-as.Date(df$MonthYear, format = "%b-%y")
Output:
MonthYear
1 <NA>
2 <NA>
3 <NA>
So I used the following code and added day as well. But I don't want the date:
df<- data.frame(MonthYear = c("Jan-20", "Feb-19", "May-18"))
df$MonthYear<-paste(as.character(df$MonthYear))
df$MonthYear<-paste("01-",as.character(df$MonthYear))
df$MonthYear<-as.Date(df$MonthYear, format = "%d- %b-%y")
Output:
MonthYear
1 2020-01-01
2 2019-02-01
3 2018-05-01
But my required output is as follows:
MonthYear
1 2020-01
2 2019-02
3 2018-05
Using as.yearmon()
from the zoo package (and the magrittr pipe):
library(zoo)
library(magrittr)
as.yearmon(df$MonthYear, "%b-%y") %>%
format(., "%Y-%m")
[1] "2020-01" "2019-02" "2018-05"
Can also be done without the '.' used as a placeholder for the left hand side of the pipe. It was left in as these functions aren't typical tidyverse piping functions.
as.yearmon(df$MonthYear, "%b-%y") %>%
format("%Y-%m")
Or without piping at all, and using nested functions (as pointed out by @Sotos). I find them harder to read, and usually have the tidyverse (and therefore %>% pipes) loaded anyway.
format(as.yearmon(df$MonthYear, "%b-%y"), "%Y-%m")
You could paste an arbitrary date, convert to Date and then use format
format(as.Date(paste0("01-",df$MonthYear), "%d-%b-%y"), "%Y-%m")
#[1] "2020-01" "2019-02" "2018-05"
Maybe you can try the following using gsub()
to keep the year and month only, ie,
format(gsub("(.*)-\\d+","\\1",df$MonthYear),format = "%Y-%m")
or just
gsub("(.*)-\\d+","\\1",df$MonthYear)
such that
[1] "2020-01" "2019-02" "2018-05"
DATA
df <- structure(list(MonthYear = structure(c(18262, 17928, 17652), class = "Date")), class = "data.frame", row.names = c(NA,
-3L))
x <- c("Jan-20", "Feb-19", "May-18")
for (i in seq_along(month.abb)) x <- sub(month.abb[i], sprintf("%02d", i), x)
# Can add fixed = TRUE for speed
x
# [1] "01-20" "02-19" "05-18"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.