How can I simplify my dplyr/apply script?

81 Views Asked by At

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:

  1. change all dates to be the same for each row of the three columns (leave times untouched

  2. 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.

1

There are 1 best solutions below

3
On BEST ANSWER

Here is a dplyr solution to the problem you described:

library(magrittr)
library(dplyr)
library(stringr)
library(lubridate)

date.zero<- ymd_hms("2018-01-01 00:00:00", tz = "America/New_York")

new_df <- df %>% # 1) change all dates to be the same for each row of the three columns
    mutate(tim.col.1 = ymd_hms(str_replace(tim.col.1, "\\S+", "2018-01-01"), tz = "America/New_York"),
          tim.col.2 = ymd_hms(str_replace(tim.col.2, "\\S+", "2018-01-01"), tz = "America/New_York"),
          tim.col.3 = ymd_hms(str_replace(tim.col.3, "\\S+", "2018-01-01"), tz = "America/New_York")) %>%
    # 2) calculate difference in time between actual time in the column/row against a 
    # nominal date/time combination which yields three new columns with seconds
    mutate(tim.col.1 = tim.col.1 - date.zero,
           tim.col.2 = tim.col.2 - date.zero,
           tim.col.3 = tim.col.3 - date.zero)

Edit: And here is the mutate_if version based on Moody_Mudskipper's suggestion:

new_df <- df %>% # 1) change all dates to be the same for each row of the three columns
    mutate_if(is.POSIXct, funs(ymd_hms(str_replace(., "\\S+", "2018-01-01"), tz = "America/New_York"))) %>%
    # 2) calculate difference in time between actual time in the column/row against a 
    # nominal date/time combination which yields three new columns with seconds
    mutate_if(is.POSIXct, funs(. - date.zero))