-1

As you can see I have two df1 databases. For the first one it worked I generate my output table. However if I try for second df1 database it doesn't work. This is probably because the date 01/07 of the ABC category has all its columns equal to 0, and this ends up giving a problem with the datas. However, I would like a way to generate the output table anyway, in this case the table would be generated, but it would not have the row corresponding to that day (01/07 ABC category). Is there any way to make a condition or something?

Executable code below:

library(dplyr)

df1 <- structure(
  list(date1= c("2021-06-28","2021-06-28","2021-06-28","2021-06-28"),
       date2 = c("2021-06-30","2021-06-30","2021-07-01","2021-07-01"),
       Category = c("FDE","ABC","FDE","ABC"),
       Week= c("Wednesday","Wednesday","Friday","Friday"),
       DR1 = c(4,1,6,1),
       DR01 = c(4,1,4,3), DR02= c(4,2,6,0),DR03= c(9,5,4,0),
       DR04 = c(5,4,3,3),DR05 = c(5,4,5,0),
       DR06 = c(2,4,3,3),DR07 = c(2,5,4,0),
       DR08 = c(3,4,5,0),DR09 = c(2,3,4,0)),
  class = "data.frame", row.names = c(NA, -4L))

#df1 <- structure(
 # list(date1= c("2021-06-28","2021-06-28","2021-06-28","2021-06-28"),
  #     date2 = c("2021-06-30","2021-06-30","2021-07-01","2021-07-01"),
   #    Category = c("FDE","ABC","FDE","ABC"),
    #   Week= c("Wednesday","Wednesday","Friday","Friday"),
     #  DR1 = c(4,1,6,0),
      # DR01 = c(4,1,4,0), DR02= c(4,2,6,0),DR03= c(9,5,4,0),
      # DR04 = c(5,4,3,0),DR05 = c(5,4,5,0),
      # DR06 = c(2,4,3,0),DR07 = c(2,5,4,0),
      # DR08 = c(3,4,5,0),DR09 = c(2,3,4,0)),
  #class = "data.frame", row.names = c(NA, -4L))

return_coef <- function(dmda, CategoryChosse) {
  
  x<-df1 %>% select(starts_with("DR0"))
  
  x<-cbind(df1, setNames(df1$DR1 - x, paste0(names(x), "_PV")))
  PV<-select(x, date2,Week, Category, DR1, ends_with("PV"))
  
  med<-PV %>%
    group_by(Category,Week) %>%
    summarize(across(ends_with("PV"), median))
  
  SPV<-df1%>%
    inner_join(med, by = c('Category', 'Week')) %>%
    mutate(across(matches("^DR0\\d+$"), ~.x + 
                    get(paste0(cur_column(), '_PV')),
                  .names = '{col}_{col}_PV')) %>%
    select(date1:Category, DR01_DR01_PV:last_col())
  
  SPV<-data.frame(SPV)
  
  mat1 <- df1 %>%
    filter(date2 == dmda, Category == CategoryChosse) %>%
    select(starts_with("DR0")) %>%
    pivot_longer(cols = everything()) %>%
    arrange(desc(row_number())) %>%
    mutate(cs = cumsum(value)) %>%
    filter(cs == 0) %>%
    pull(name)
  
  (dropnames <- paste0(mat1,"_",mat1, "_PV"))
  
  SPV <- SPV %>%
    filter(date2 == dmda, Category == CategoryChosse) %>%
    select(-any_of(dropnames))
  
  datas<-SPV %>%
    filter(date2 == ymd(dmda)) %>%
    group_by(Category) %>%
    summarize(across(starts_with("DR0"), sum)) %>%
    pivot_longer(cols= -Category, names_pattern = "DR0(.+)", values_to = "val") %>%
    mutate(name = readr::parse_number(name))
  colnames(datas)[-1]<-c("Days","Numbers")
  
  datas <- datas %>% 
    group_by(Category) %>% 
    slice((as.Date(dmda) - min(as.Date(df1$date1) [
      df1$Category == first(Category)])):max(Days)+1) %>%
    ungroup
  
  mod <- nls(Numbers ~ b1*Days^2+b2,start = list(b1 = 0,b2 = 0),data = datas, algorithm = "port")
  as.numeric(coef(mod)[2])
  
}

cbind(df1[2:3], coef = mapply(return_coef, df1$date2, df1$Category))

> cbind(df1[2:3], coef = mapply(return_coef, df1$date2, df1$Category))

           date2 Category coef
    1 2021-06-30      FDE    4
    2 2021-06-30      ABC    1
    3 2021-07-01      FDE    6
    4 2021-07-01      ABC    1

1 Answers1

0

You may use an if/else condition to check.

return_coef <- function(dmda, CategoryChosse) {
  
  x<-df1 %>% select(starts_with("DR0"))
  
  x<-cbind(df1, setNames(df1$DR1 - x, paste0(names(x), "_PV")))
  PV<-select(x, date2,Week, Category, DR1, ends_with("PV"))
  
  med<-PV %>%
    group_by(Category,Week) %>%
    summarize(across(ends_with("PV"), median))
  
  SPV<-df1%>%
    inner_join(med, by = c('Category', 'Week')) %>%
    mutate(across(matches("^DR0\\d+$"), ~.x + 
                    get(paste0(cur_column(), '_PV')),
                  .names = '{col}_{col}_PV')) %>%
    select(date1:Category, DR01_DR01_PV:last_col())
  
  SPV<-data.frame(SPV)
  
  mat1 <- df1 %>%
    filter(date2 == dmda, Category == CategoryChosse) %>%
    select(starts_with("DR0")) %>%
    pivot_longer(cols = everything()) %>%
    arrange(desc(row_number())) %>%
    mutate(cs = cumsum(value)) %>%
    filter(cs == 0) %>%
    pull(name)
  
  (dropnames <- paste0(mat1,"_",mat1, "_PV"))
  
  SPV <- SPV %>%
    filter(date2 == dmda, Category == CategoryChosse) %>%
    select(-any_of(dropnames))
  if(any(grep('DR0', names(SPV)))) {
    
  datas<-SPV %>%
    filter(date2 == ymd(dmda)) %>%
    group_by(Category) %>%
    summarize(across(starts_with("DR0"), sum)) %>%
    pivot_longer(cols= -Category, names_pattern = "DR0(.+)", values_to = "val") %>%
    mutate(name = readr::parse_number(name))
  colnames(datas)[-1]<-c("Days","Numbers")
  
  datas <- datas %>% 
    group_by(Category) %>% 
    slice((as.Date(dmda) - min(as.Date(df1$date1) [
      df1$Category == first(Category)])):max(Days)+1) %>%
    ungroup
  
  mod <- nls(Numbers ~ b1*Days^2+b2,start = list(b1 = 0,b2 = 0),data = datas, algorithm = "port")
  as.numeric(coef(mod)[2])
  } else {
    return(NA)
  }
  
}

This returns -

res <- mapply(return_coef, df1$date2, df1$Category)
cbind(df1[!is.na(res), 2:3], coef = na.omit(res))

#       date2 Category coef
#1 2021-06-30      FDE    4
#2 2021-06-30      ABC    1
#3 2021-07-01      FDE    6
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Instead of `2:3` in `cbind`, it could be something: `df1%>% select(date2,Category)`? –  Oct 13 '21 at 03:09
  • Yes, like this `cbind(df1 %>% select(date2,Category) %>% filter(!is.na(res)), coef = na.omit(res))` – Ronak Shah Oct 13 '21 at 03:14
  • Ronak, could you please take a look at this question: https://stackoverflow.com/questions/69907443/how-to-adjust-error-when-i-have-0-values-for-graph-generation I think it's similar to this question, but I'm generate graphs of the results, and it's not working very well to values equal to 0. –  Nov 10 '21 at 03:27