in Education by
I would like some help to adjust the output called adjusted that I generate. My idea is to optimize somehow to generate faster. Notice that I'm using pivot_longer, which takes longer. One idea would be to continue using data.table as I did to generate SPV. However, I don't know how to do that in this case for adjusted. Can you help me? I would like to generate the same output table as in the question. library(dplyr) library(tidyr) library(lubridate) library(data.table) df1 <- structure( list(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-25","2021-06-25","2021-06-27","2021-07-07","2021-07-07","2021-07-09","2021-07-09","2021-07-09"), Code = 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), DR01 = c(4,1,4,3,3,4,3,6), DR02= c(4,2,6,7,3,2,7,4),DR03= c(9,5,4,3,3,2,1,5), DR04 = c(5,4,3,3,6,2,1,9),DR05 = c(5,4,5,3,6,2,1,9), DR06 = c(2,4,3,3,5,6,7,8),DR07 = c(2,5,4,4,9,4,7,8), DR08 = c(0,0,0,1,2,0,0,0),DR09 = c(0,0,0,0,0,0,0,0),DR010 = c(0,0,0,0,0,0,0,0),DR011 = c(4,0,0,0,0,0,0,0), DR012 = c(0,0,0,3,0,0,0,5),DR013 = c(0,0,1,0,0,0,2,0),DR014 = c(0,0,0,0,0,2,0,0)), class = "data.frame", row.names = c(NA, -8L)) selection = startsWith(names(df1), "DRM") df1[selection][is.na(df1[selection])] = 0 dt1 <- as.data.table(df1) cols <- grep("^DR0", colnames(dt1), value = TRUE) medi_ana <- dt1[, (paste0(cols, "_PV")) := DR1 - .SD, .SDcols = cols ][, lapply(.SD, median), by = .(Code, Week), .SDcols = paste0(cols, "_PV") ] f1 <- function(nm, pat) grep(pat, nm, value = TRUE) nm1 <- f1(names(df1), "^DR0\\d+$") nm2 <- f1(names(medi_ana), "_PV") nm3 <- paste0("i.", nm2) setDT(df1)[medi_ana, (nm2) := Map(`+`, mget(nm1), mget(nm3)), on = .(Code, Week)] SPV <- df1[, c('date1', 'date2', 'Code', 'Week', nm2), with = FALSE] dmda<-"2021-07-09" code<-"CDE" adjusted<-SPV %>% filter(date2==dmda,Code == code) %>% group_by(Code) %>% summarize(across(starts_with("DR0"), sum),.groups = 'drop') %>% pivot_longer(cols= -Code, names_pattern = "DR0(.+)", values_to = "val") %>% mutate(name = readr::parse_number(name)) > adjusted # A tibble: 14 x 3 Code name val 1 CDE 1 5 2 CDE 2 5 3 CDE 3 5 4 CDE 4 5 5 CDE 5 5 6 CDE 6 5 7 CDE 7 5 8 CDE 8 5 9 CDE 9 5 10 CDE 10 5 11 CDE 11 5 12 CDE 12 5 13 CDE 13 5 14 CDE 14 5 JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
With data.table, use melt after doing the filtering and grouping library(data.table) melt(SPV[date2 == dmda & Code == code][, lapply(.SD, sum, na.rm = TRUE), by = Code, .SDcols = patterns("^DR0")], id.var = "Code", variable.name = "name", value.name = "val")[, name := readr::parse_number(as.character(name))][] -output Code name val 1: CDE 1 5 2: CDE 2 5 3: CDE 3 5 4: CDE 4 5 5: CDE 5 5 6: CDE 6 5 7: CDE 7 5 8: CDE 8 5 9: CDE 9 5 10: CDE 10 5 11: CDE 11 5 12: CDE 12 5 13: CDE 13 5 14: CDE 14 5 Or using the R chain (|>) SPV[date2 == dmda & Code == code] |> {\(.) .[, lapply(.SD, sum, na.rm = TRUE), by = Code, .SDcols = patterns("^DR0")]}() |> melt(id.var = "Code", variable.name = "name", value.name = "val") |> {\(.) .[, name := readr::parse_number(as.character(name))][]}()

Related questions

0 votes
    I would like some help to adjust the output called adjusted that I generate. My idea is to optimize somehow to ... dplyr) library(tidyr) library(lubridate) library(data.table) df1...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    __________ is used when you have variables that form rows instead of columns. (a) tidy() (b) spread() (c ... Analysis of R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    __________ uses regex groups instead of a splitting pattern or position. (a) spread() (b) gather() (c) ... Analysis of R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    Unlike writing out a table or CSV file, dump() and dput() preserve the ______ so that another user ... Operations of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    Which of the following adds marginal sums to an existing table? (a) par() (b) prop.table() (c) ... Linear Regression of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    Which of the following compute analysis of variance table for fitted model? (a) ecdf() (b) cum() (c) ... Regression of R Programming Select the correct answer from above options...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    Which of the following compute proportions from a contingency table? (a) par() (b) prop.table() (c) ... Regression of R Programming Select the correct answer from above options...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    The purpose of fisher.test() is _______ test for contingency table. (a) Chisq (b) Fisher (c) Prop (d ... Regression of R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    For barchart and _________ non-trivial methods exist for tables and arrays, documented at barchart.table. (a) ... of R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    I can think of two main benefits: Avoiding concurrency problems, if you have many processes creating/dropping ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    The most convenient way to use R is at a graphics workstation running a ________ system. (a) windowing (b) ... Started of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    What should we use to access elements with a value greater than five? (a) Subsetting commands (b) Use ... Out of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    In order to use the R-related functionality in Dundas BI, you must have access to an existing _________ (a ... Started of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    If you have a lot of objects that you want to save to a file, we use ________ function. (a) save() ... and Operations of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    To extract a sublist, we use _________ brackets. (a) Flower (b) Square (c) Double (d) Single This question ... and Out of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
...