2

I have following dataframe in r

  Date                    Count   
  2016-12-30 17:25:00      34
  2016-12-04 10:25:00      31

I want to change the format to following and want to remove seconds from Date

Desired Dataframe would be

  Date                 Count
  30-12-2016 17:25     34
  04-12-2016 10:25     31

I am doing following but because of space inbetween date it is returning me wrong output

 substr(df$Date,1,nchar(df$Date)-3)

How can I do it in R?

Neil
  • 7,937
  • 22
  • 87
  • 145

1 Answers1

2

We convert it to POSIXct and then do the format

df1$Date <- format(as.POSIXct(df1$Date), "%d-%m-%Y %H:%M")
df1$Date
#[1] "30-12-2016 17:25" "04-12-2016 10:25"
akrun
  • 874,273
  • 37
  • 540
  • 662
  • That works, but what if I want to trim last 3 letters in above case which includes spaces,how can we do it? – Neil Sep 11 '17 at 06:44
  • @Neil If the format is not an issue, then use `substr` i.e. `substring(trimws(df1$Date), 1, nchar(df1$Date)-3)` – akrun Sep 11 '17 at 06:46