Search code examples
rtimetimeline

r - Adding time column to data frame based on time count


In my data I have a column of temperature measurements together with a column showing the time passed until the temp was measured by the data logger. The dataframe contains 500000+ observations.

eggtemp <- read.csv("temp_time.csv", header=FALSE)
colnames(eggtemp) <- c("time passed", "temp")

   time passed   temp      
1  0.00043823    16.876    
2  0.00087645    17.903    
3  0.00131470    18.923    
4  0.00175290    19.933    

The data logger started recording at 7/30/2014, 13:05:00.

Is there a way to make a new column in the data set, based on the start recording time, showing the exact time at which the measurements were taken? Like this one (I typed the rough time in by hand for the first 4 rows):

   time passed   temp      time
1  0.00043823    16.876    13:05:00:00
2  0.00087645    17.903    13:05:00:04
3  0.00131470    18.923    13:05:00:09
4  0.00175290    19.933    13:05:00:13

Can someone tell me if there is a function that takes the starting time and adds the "time passed" values, to get the exact time (and date) of every observation? I just can't figure it out..

Thanks!


Solution

  • You don't mention the unit of eggtemp[,'time passed'], but I'm guessing hours from your typed in values. I use the lubridate package, it makes these calculations easy. Although it's definitely possible with base R.

    start <- mdy_hms("7/30/2014 13:05:00")
    time_passed <- c(0.00043823, 0.00087645, 0.00131470, 0.00175290)
    time_passed <- dhours(time_passed)
    time <- start + time_passed
    

    Make sure to set the tz argument to mdy_hms if you're concerned about getting it into a correct local time.