0

Note that for some dates/categories, the coefficient values are negative, as for 06/07/2021 in category FDE and ABC, which gave -0.74 and -0.51. So, I would like to know if it is possible that when the coefficients are negative, these values are replaced by 0. How can I do this?

    library(dplyr)

    df1 <- structure(
      list(date1= c("2021-06-28","2021-06-28","2021-06-28","2021-06-28","2021-06-28"),
           date2 = c("2021-06-29","2021-06-29","2021-07-06","2021-07-06","2021-07-06"),
           Category = c("FDE","ABC","FDE","ABC","DDE"),
           Week= c("Tuesday","Tuesday","Tuesday","Tuesday","Tuesday"),
           DR1 = c(4,1,0,2,2),
           DR01 = c(4,1,0,3,2), DR02= c(4,2,0,2,4),DR03= c(9,5,0,7,1),
           DR04 = c(5,4,0,2,1),DR05 = c(5,4,0,4,1),
           DR06 = c(2,4,0,2,4),DR07 = c(2,5,3,4,5),
           DR08 = c(3,4,5,4,4),DR09 = c(2,3,7,4,5),DR10 = c(2,3,9,4,5),DR13 = c(2,3,10,4,5)),
      class = "data.frame", row.names = c(NA, -5L))
    
    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)])-2):max(Days)+1) %>%
        ungroup
      
        m<-df1 %>%
        group_by(Category,Week) %>%
        summarize(across(starts_with("DR1"), mean))
      
       m<-subset(m, Week == df1$Week[match(ymd(dmda), ymd(df1$date2))] & Category == CategoryChosse)$DR1
      
       if (nrow(datas)<=2){
       as.numeric(m)
       }
      
      else{
      mod <- nls(Numbers ~ b1*Days^2+b2,start = list(b1 = 0,b2 = 0),data = datas, algorithm = "port")
      as.numeric(coef(mod)[2])
            }
    }
    
    cbind(df1 %>% select(date2, Category), coef = mapply(return_coef, df1$date2, df1$Category))

        date2 Category       coef
1 2021-06-29      FDE  5.3478916
2 2021-06-29      ABC  1.3694779
3 2021-07-06      FDE -0.7451236
4 2021-07-06      ABC -0.5182055
5 2021-07-06      DDE  2.0000000
Antonio
  • 1,091
  • 7
  • 24
  • 1
    Simple subscripting should suffice. Assign your final results to a variable like so: `results <- cbind(df1 %>% ...)`. Then target the negative coefficients in the `coef` column and overwrite them with `0`: `results$coef[results$coef < 0] <- 0`. – Greg Oct 11 '21 at 15:26
  • @Greg, please post as an answer? – Ben Bolker Oct 11 '21 at 15:27
  • 1
    @BenBolker I appreciate the thought, but this is a pretty basic operation in R, so it's almost certainly a duplicate of another answer elsewhere. In such cases, I try to just help in the comments without trawling for rep. – Greg Oct 11 '21 at 15:29
  • This question may be helpful: https://stackoverflow.com/questions/35610437/using-dplyr-to-conditionally-replace-values-in-a-column – Anis R. Oct 11 '21 at 15:46

0 Answers0