3

I have a data frame of date, time. I read it with as.POSIXct, but i don't need the dates, i only need the time. then i want to make box plot with the times that i have. how i can only read the time data?

C1
   13/07/2017 15:15
   13/07/2017 15:02
   13/07/2017 14:40
   13/07/2017 15:15
   13/07/2017 15:10
   13/07/2017 15:23
   13/07/2017 14:56
   13/07/2017 15:15
   13/07/2017 15:15
   13/07/2017 14:56
   13/07/2017 14:56
   13/07/2017 14:56
   13/07/2017 15:15
   13/07/2017 15:02
   13/07/2017 15:10

df <-  read.csv2(file="DF.csv")


df <- as.POSIXct(df$C1, format="%H:%M:%S", tz="GMT")
zx8754
  • 52,746
  • 12
  • 114
  • 209
Mori
  • 241
  • 1
  • 2
  • 10
  • You need to `format` the variable after reading it as `POSIXct`. Example: `as.POSIXct(df$C1, format = "%d/%m/%Y %H:%M") %>% format("%H:%M:%S")` – amatsuo_net Jul 13 '17 at 08:08
  • i formatted but still the dates will be appear – Mori Jul 13 '17 at 08:16
  • Once you use `format()` function, the output is a string of HMS, so that can't happen. – amatsuo_net Jul 13 '17 at 08:35
  • Possible duplicate of [date time conversion and extract only time](https://stackoverflow.com/questions/31876962/date-time-conversion-and-extract-only-time) – JanLauGe Jul 13 '17 at 08:54

2 Answers2

3
library(lubridate)
df$C1 <- hm(df$C1)
Kalees Waran
  • 659
  • 6
  • 13
3

I tried to replicate your data frame and apply the function as.POSIXct() to better replicate your situation.

You can use the strftime() function to split the column and then the function times() to create a chronological object.

df = data.frame(C1 = c("13/07/2017 15:15","13/07/2017 15:02","13/07/2017 14:40","13/07/2017 15:15",
                   "13/07/2017 15:10","13/07/2017 15:23","13/07/2017 14:56","13/07/2017 15:15",
                   "13/07/2017 15:15","13/07/2017 14:56","13/07/2017 14:56","13/07/2017 14:56",
                   "13/07/2017 15:15","13/07/2017 15:02","13/07/2017 15:10"))  #replicate data frame
df <- as.POSIXct(df$C1, format = "%d/%m/%Y %H:%M") #apply  function to create a POSIXct object

Then the function you need to apply to your data frame it's the following:

library(chron)
time <- times(strftime(df, format="%H:%M:%S")) #select only time and create a times object

This is results:

time
[1] 15:15:00 15:02:00 14:40:00 15:15:00 15:10:00 15:23:00 14:56:00 15:15:00 15:15:00 14:56:00 14:56:00 14:56:00 15:15:00 15:02:00 15:10:00
class(time)
[1] "times"
Lorenzo Benassi
  • 621
  • 1
  • 8
  • 31