0

How can I change the datatype in my dataframe from chr to time, without adding dates to it?

My dataframe is called exit_count and my column is called time.

When I use the head(exit_count) I can see the table how it is supposed to, however when I use the str(exit_count) I can see that my time coulmn is in chr-format.

Programing in R:

I have tried using the as.POSIXct():

exit_count$time <- as.POSIXct(exit_count$time,format="%H:%M")

however, this adds today's date which messes up with my dataset (I have a separate column for date).

I have tried the chron-package:

chron(times = exit_count$time, format="%H:%M")

however, it still doesn't work.

exit_count <- read.csv (Data/Exit count/exit_count_all_data.csv",
                  sep=";", dec=".", header=TRUE, stringsAsFactors=FALSE)

exit_count$date <- as.Date(as.POSIXct(exit_count$date, format="%d.%m.%Y", tz="GMT")) #This works


exit_count$time <- chron(times = exit_count$time,format="%H:%M") #However, neither this

exit_count$time <- as.POSIXct(exit_count$time,format="%H:%M") #Nor this

exit_count$time <- times(paste0(exit_count$date, "%H:%M"))    #Nor this works (This was something that @Sotos wanted me to try, but it still doesn't work).

exit_count$time <- chron(times=exit_count$time) #Nor this. However, i get an output here - but this yields big numbers (e.g. 17911.93), which is not what I want.

I need for this to work in order to get my data frames and my plots to be correct for my masters thesis. Without me being an expert, I imagine it can be done in such a way that the date and the time can be in two separate columns and that the time can be in a datatype that R understands as time (like it does with the POSIX and date, that I have already manage to make).

Please help! Thanks :)

Karoline
  • 1
  • 3
  • 1
    Why do you need the date and time to be in separate columns? It may be that there's another solution to your problem rather than creating a time column. – A. Stam Jan 14 '19 at 12:22
  • As in `lubridate::hm("12:24")`? – Rui Barradas Jan 14 '19 at 12:24
  • They are already in two separate columns. The problem is that whenever I add the time column by it's own, it gets merged with a new todays date (e.g. 15.01.2019), however, all my data were collected last summer (2018). – Karoline Jan 15 '19 at 14:22

0 Answers0