convert datetime to three letter month and year in R

Viewed 1278

I need help to convert the first column of datetime in 3 letter month and year.

DF<-

Datetime              ID      Name
2020-01-01 10:12:14   I-1     Rnad
2020-01-01 16:32:43   I-2     Rnxa

Required output

Datetime              ID      Name   Month
2020-01-01 10:12:14   I-1     Rnad   Jan-20
2020-01-01 16:32:43   I-2     Rnxa   Jan-20
2 Answers

You can use the format function with strptime abbreviations.

my_df$Month <- format(my_df$Datetime, format = "%b-%y"

Try this Sophia:

#Code
df$Month <- format(as.POSIXct(df$Datetime,format='%Y-%m-%d %H:%M:%S',
                              tz = 'GMT'),"%b-%y")

Output:

df
             Datetime  ID Name  Month
1 2020-01-01 10:12:14 I-1 Rnad Jan-20
2 2020-01-01 16:32:43 I-2 Rnxa Jan-20

Some data used:

#Data
df <- structure(list(Datetime = c("2020-01-01 10:12:14", "2020-01-01 16:32:43"
), ID = c("I-1", "I-2"), Name = c("Rnad", "Rnxa")), row.names = c(NA, 
-2L), class = "data.frame")
Related