Search code examples
rdata-cleaning

How to remove the date from a column containing both date and time using R


I have read a csv file

input <- read.csv("abc.csv",header=FALSE)

and the data frame looks like..

look here

i want my expected result like this.. in the timeStamp column i want to replace "2017/03/10 08:35:07.996" to only "08:35:07.996".

Tried a lot but could find any solution.Please help


Solution

  • We can set the digits.secs to 3, then convert the 'timeStamp' to DateTime class with strptime and format it

    op <- options(digits.secs=3)
    input$timeStamp <- format(strptime(input$timeStamp, "%Y/%m/%d %H:%M:%OS"), "%H:%M:%OS")
    

    Though, it is better not to use regex on timestamps, one way is to match one or more non-white space (\\S+) character followed by one or more white space (\\s+) from the start (^) of the string and replace it with blanks ("") so that the rest of the string i.e. time part remains

    input$timeStamp <- sub("^\\S+\\s+", "", input$timeStamp)