2

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
markus
  • 25,843
  • 5
  • 39
  • 58
Sophia Wilson
  • 581
  • 3
  • 16

2 Answers2

2

You can use the format function with strptime abbreviations.

my_df$Month <- format(my_df$Datetime, format = "%b-%y")
mrhellmann
  • 5,069
  • 11
  • 38
1

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")
Duck
  • 39,058
  • 13
  • 42
  • 84