31

Suppose I have a variable like this

c<-c("9/21/2011 0:00:00",  "9/25/2011 0:00:00",  "10/2/2011 0:00:00",  
"9/28/2011 0:00:00",  "9/27/2011 0:00:00")

what's a quick way to remove all 0:00:00s so that

c
[1] "9/21/2011" "9/25/2011" "10/2/2011" "9/28/2011" "9/27/2011"
smci
  • 32,567
  • 20
  • 113
  • 146
David Z
  • 6,641
  • 11
  • 50
  • 101
  • 1
    Use the `gsub` function. There must be many, many worked examples in SO archives. – IRTFM Apr 15 '14 at 16:54
  • May you give an answer here using the example? +1 – David Z Apr 15 '14 at 16:56
  • 2
    This may work `sapply(strsplit(x, "\\s+"), "[", 1)` but you may want to actually work with this data as a date (`as.Date`). – Tyler Rinker Apr 15 '14 at 16:57
  • 2
    I think the question isn't asking for full generality, only *How to remove time-field string from a date-as-character variable?* – smci Apr 15 '14 at 18:44

3 Answers3

27

You can turn them into dates and then format as desired, e.g.:

v <- c("9/21/2011 0:00:00",  "9/25/2011 0:00:00",  "10/2/2011 0:00:00",  
     "9/28/2011 0:00:00",  "9/27/2011 0:00:00")
v <- format(as.POSIXct(v,format='%m/%d/%Y %H:%M:%S'),format='%m/%d/%Y')
> v
[1] "09/21/2011" "09/25/2011" "10/02/2011" "09/28/2011" "09/27/2011"

Or, you can simply remove the " 0:00:00" substring using gsub:

v <- gsub(x=v,pattern=" 0:00:00",replacement="",fixed=T)
> v
[1] "9/21/2011" "9/25/2011" "10/2/2011" "9/28/2011" "9/27/2011"
digEmAll
  • 56,430
  • 9
  • 115
  • 140
  • 11
    a simpler way could be to use [Round](https://stat.ethz.ch/R-manual/R-devel/library/base/html/round.POSIXt.html) with `unit="day"` on date types – Aramis7d Nov 27 '15 at 05:06
  • 1
    use [round_date](https://lubridate.tidyverse.org/reference/round_date.html) for date-time objects in `lubridate` – cengel May 17 '21 at 22:56
  • @cengel: well, the example of the OP wasn't talking about dates, but strings containing dates, so I'd prefer to avoid loading an external package just to remove some suffix from a string... – digEmAll May 18 '21 at 08:25
  • 1
    A great tip to remove the date part from date-time. I just modified the code like this: `v <- format(as.POSIXct(v,format='%m/%d/%Y %H:%M:%S'),format='%H:%M:%S')` – Mehmet Yildirim Aug 04 '22 at 18:17
11

From the lubridate package: Use mdy_hms() to read in the characters as Month, Day, Year and Hours, Minutes, Seconds, then wrap with as.Date() to strip the time.

library(lubridate)
v <- c("9/21/2011 0:00:00",  "9/25/2011 0:00:00",  "10/2/2011 0:00:00",  
       "9/28/2011 0:00:00",  "9/27/2011 0:00:00")
v <- as.Date(mdy_hms(v))
v
# [1] "2011-09-21" "2011-09-25" "2011-10-02" "2011-09-28" "2011-09-27"

If you want to maintain the vector as character type, not date type:

v <- as.character(as.Date(mdy_hms(v)))
Kayle Sawyer
  • 549
  • 7
  • 22
2

Keeping original class as character. Use gsub (or sub) to remove everything after space. The .* pattern will find the first space.

c<-c("9/21/2011 0:00:00",  "9/25/2011 0:00:00",  "10/2/2011 0:00:00",  
     "9/28/2011 0:00:00",  "9/27/2011 0:00:00")

gsub(" .*","", c)
[1] "9/21/2011" "9/25/2011" "10/2/2011" "9/28/2011" "9/27/2011"
DAY
  • 91
  • 6