I want to go through several steps using a df which contains POSIXct datapoints.
Essentially there are three columns in a dataframe which have different dates. The following needs to be achieved:
change all dates to be the same for each row of the three columns (leave times untouched
calculate difference in time between actual time in the column/row against a nominal date/time combination which yields three new columns with seconds
I have done this successfully but my answer (which I already sought help on) seems too long and cumbersome, here it is:
The first thing I did was to create a nominal date to use in calculations:
date.zero<- as.POSIXct("2018-01-01 00:00:00 EST")
I then changed all dates in each row of the data frame within the specific columns to the same date
df$tim.col.1 <- as.POSIXct(sub("\\S+", "2018-01-01", df$tim.col.1))
df$tim.col.2 <- as.POSIXct(sub("\\S+", "2018-01-01", df$tim.col.2))
df$tim.col.2 <- as.POSIXct(sub("\\S+", "2018-01-01", df$tim.col.2))
Lastly I used lapply to subtract the dates from the date.zero to yield time difference in seconds (i.e. essentially seconds from 00:00:00)
df["tim.col.1"] <- lapply(df["tim.col.1"],function(x) x-date.zero)
df["tim.col.2"] <- lapply(df["tim.col.2"],function(x) x-date.zero)
df["tim.col.3"] <- lapply(df["tim.col.3"],function(x) x-date.zero)
Now. I'm guessing that all of this can easily be either done using lapply in a better fashion or using dplyr so I don't need to type all this code...using something like this perhaps but integrating everything together?
newdf <- df %>% rowwise () %>% mutate(xxx=tim.col.1-date.zero,
xxx2=tim.col.2-date.zero,
xxx3=tim.col.3-date.zero)
Can someone enlighten me as to how this would be achieved most succinctly and efficiently.