I would like to take a doubt with you, as well as adjust, if necessary. See that I generate a result
value for each Id, Category and Week
in the function. To find this result value I use the median, which is med
variable. After inserting the function, I do:
df1 %>%
rowwise %>%
mutate(example(df1,Id,Category, Week)) %>%
select(-c(6:20,))%>%
data.frame()
In this case it generates values for all id/category and week
. So, my question is: When executing this line above, it means that I want to generate values for all id/date/week
, so the function will be executed several times. But notice that med
does not vary, it is always the same, so it could only be done once. What possibilities do I have for adjustments so I don't keep repeating it several times?
Executable code:
library(tidyverse)
library(lubridate)
library(data.table)
df1 <- structure(
list(Id=c("1","1","1","1","1","1","1","1"),
date1= c("2021-06-28","2021-06-28","2021-06-28","2021-06-28","2021-06-28",
"2021-06-28","2021-06-28","2021-06-28"),
date2 = c("2021-06-30","2021-06-30","2021-07-02","2021-07-04","2021-07-04","2021-07-09","2021-07-09","2021-07-09"),
Category = c("FDE","ABC","ABC","ABC","CDE","FGE","ABC","CDE"),
Week= c("Wednesday","Wednesday","Friday","Wednesday","Wednesday","Friday","Friday","Friday"),
DR1 = c(4,1,4,3,3,4,3,5),
DRM01 = c(4,1,4,3,3,4,3,6), DRM02= c(4,2,6,7,3,2,7,4),DRM03= c(9,5,4,3,3,2,1,5),
DRM04 = c(5,4,3,3,6,2,1,9),DRM05 = c(5,4,5,3,6,2,1,9),
DRM06 = c(2,4,3,3,5,6,7,8),DRM07 = c(2,5,4,4,9,4,7,8),
DRM08 = c(0,0,0,1,2,0,0,0),DRM09 = c(0,0,0,0,0,0,0,0),DRM010 = c(0,0,0,0,0,0,0,0),DRM011 = c(4,0,0,0,0,0,0,0),
DRM012 = c(0,0,0,3,0,0,0,5),DRM013 = c(0,0,1,0,0,0,2,0),DRM014 = c(0,0,0,0,0,2,0,0)),
class = "data.frame", row.names = c(NA, -8L))
example <- function (df1,idd, CategoryChosse,WK) {
dt1 <- as.data.table(df1)
cols <- grep("^DRM0", colnames(dt1), value = TRUE)
med <-
dt1[, (paste0(cols, "_PV")) := DR1 - .SD, .SDcols = cols
][, lapply(.SD, median), by = .(Id, Category, Week), .SDcols = paste0(cols, "_PV") ]
result<-med %>%
filter(Id==idd, Category == CategoryChosse,Week == WK)
return(result)
}
df1 %>%
rowwise %>%
mutate(example(df1,Id,Category, Week)) %>%
select(-c(6:20,))%>%
data.frame()
Id date1 date2 Category Week DRM01_PV DRM02_PV DRM03_PV DRM04_PV DRM05_PV DRM06_PV DRM07_PV DRM08_PV DRM09_PV DRM010_PV DRM011_PV DRM012_PV DRM013_PV
1 1 2021-06-28 2021-06-30 FDE Wednesday 0 0.0 -5 -1.0 -1.0 2.0 2.0 4.0 4.0 4.0 0.0 4.0 4
2 1 2021-06-28 2021-06-30 ABC Wednesday 0 -2.5 -2 -1.5 -1.5 -1.5 -2.5 1.5 2.0 2.0 2.0 0.5 2
3 1 2021-06-28 2021-07-02 ABC Friday 0 -3.0 1 1.5 0.5 -1.5 -2.0 3.5 3.5 3.5 3.5 3.5 2
4 1 2021-06-28 2021-07-04 ABC Wednesday 0 -2.5 -2 -1.5 -1.5 -1.5 -2.5 1.5 2.0 2.0 2.0 0.5 2
5 1 2021-06-28 2021-07-04 CDE Wednesday 0 0.0 0 -3.0 -3.0 -2.0 -6.0 1.0 3.0 3.0 3.0 3.0 3
6 1 2021-06-28 2021-07-09 FGE Friday 0 2.0 2 2.0 2.0 -2.0 0.0 4.0 4.0 4.0 4.0 4.0 4
7 1 2021-06-28 2021-07-09 ABC Friday 0 -3.0 1 1.5 0.5 -1.5 -2.0 3.5 3.5 3.5 3.5 3.5 2
8 1 2021-06-28 2021-07-09 CDE Friday -1 1.0 0 -4.0 -4.0 -3.0 -3.0 5.0 5.0 5.0 5.0 0.0 5
DRM014_PV
1 4.0
2 2.0
3 3.5
4 2.0
5 3.0
6 2.0
7 3.5
8 5.0