I have a long list of time diff calculations I want to make. Given the names of the columns are long and contain spaces (pulled from an API) I've created a table of the variable names to be used for each operation:
stage_refs <- structure(list(new.diff.var = c("time diff 1", "time diff 2",
"time diff 3", "time diff 4"), var.1 = c("time value 2", "time value 3",
"time value 4", "time value 5"), var.2 = c("time value 1", "time value 2",
"time value 3", "time value 4")), row.names = c(NA, -4L), spec = structure(list(
cols = list(new.diff.var = structure(list(), class = c("collector_character",
"collector")), var.1 = structure(list(), class = c("collector_character",
"collector")), var.2 = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), delim = ","), class = "col_spec"), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))
And a date frame of dates with missing values for where no dates are as yet recorded:
date_values <- structure(list(`time value 1` = structure(c(18993, 18993, 18993,
NA), class = "Date"), `time value 2` = structure(c(19024, NA,
19026, 19027), class = "Date"), `time value 3` = structure(c(NA,
19084, 19085, 19086), class = "Date"), `time value 4` = structure(c(19113,
19114, NA, 19116), class = "Date"), `time value 5` = structure(c(19174,
19175, 19176, 19177), class = "Date")), row.names = c(NA, -4L
), class = c("tbl_df", "tbl", "data.frame"))
In the production data set there are up to 20 diff time calculations to make, hence why I've put in a table so I can use a function such as:
library(tidyverse)
difftime_fun <- function(x, y, z) {
date_values |>
mutate(!!x = difftime(y, z))
}
... to create a new column from x by the calculation between y & z and then apply this function row-wise over stage_refs
using a loop:
for(i in 1:nrow(time_diff_stages)) {
difftime_fun(
stage_refs$new.diff.var[[i]],
stage_refs$var.1[[i]],
stage_refs$var.2[[i]]
)
What I want is as many timediff columns added to date_values
as rows in stage_refs
using the variable names from stage_refs
for assignment of the cols and for the calculations.
There are 2 places I'm stuck:
- I'm running into non-standard-evaluation problems with using the variable names in the function. I've tried combinations of
!!
,{{ }}
,eval
etc and can't make sense of it. - I suspect there's a better way than using the loop to go row-wise using
apply
or some such, but not solving (1) means it's hard to trial and error usingapply
.
I have looked at this solution, but can't make sense as to how to use for this problem.
Thanks.
For clarification the final df would have the following columns:
[1] "time value 1" "time value 2" "time value 3" "time value 4" "time value 5" "time diff 1"
[7] "time diff 2" "time diff 3" "time diff 4"