3

My question takes a general aspect comparing to which was proposed here How to remove time-field string from a date-as-character variable?.

In fact, suppose I have this date type variable:

> head(DataDia$Date)
[1] "2016-09-13 15:56:30.827" "2016-12-12 13:39:17.537" "2016-09-16 21:57:24.977" "2016-09-23 11:19:22.010"
[5] "2017-01-11 20:06:58.490" "2016-10-21 23:40:43.927"

How do I delete all time-field strings and just keep the date format. SO that I get this:

> head(DataDia$Date)
[1] "2016-09-13" "2016-12-12" "2016-09-16" "2016-09-23"
[5] "2017-01-11" "2016-10-21"

Note please that I am working on a data table. So I need a way using data.table operations.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Amir
  • 59
  • 1
  • 1
  • 7

3 Answers3

6

Just use as.Date(DataDia$Date).

drmariod
  • 11,106
  • 16
  • 64
  • 110
1

You Can use:

as.POSIXct(Df$Date,format='%Y-%m-%d',tz= "UTC")

Rahul shah
  • 185
  • 2
  • 16
0

Combining as.Date and as.character

x = c("2016-09-13 15:56:30.827", "2016-12-12 13:39:17.537", "2016-09-16 21:57:24.977", "2016-09-23 11:19:22.010",
        "2017-01-11 20:06:58.490", "2016-10-21 23:40:43.927")

y = as.character(as.Date(x, format = "%Y-%m-%d"))

y
[1] "2016-09-13" "2016-12-12" "2016-09-16" "2016-09-23" "2017-01-11" "2016-10-21"
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Edu
  • 903
  • 6
  • 17