How to read only the time in a data frame
I have a date, time data frame. I read it with asp.POSIXct but I don't need dates, I only need time. then I want to make a graph with the time that I have. how can i read only 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")
+3
source to share
2 answers
I tried to replicate your dataframe and apply a function as.POSIXct()
to better replicate your situation.
You can use a function strftime()
to break up a column and then a 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 following is needed for the function to be applied to your data:
library(chron)
time <- times(strftime(df, format="%H:%M:%S")) #select only time and create a times object
These are the 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"
+3
source to share