2

The code below generates a graph according to the day/category I choose on my date2. The days are 30/06, 01/07 and 02/07. For 30/06 and 01/07, I can generate normally as you can see in the attached image, but not for 02/07. This is because all my columns have 0 values and it ends up generating a problem in datas. So I need that if all columns are 0, I would like my graph to consider this condition of the code:

if (nrow(datas)<=2){
abline(h=m,lwd=2) 
points(0, m, col = "red", pch = 19, cex = 2, xpd = TRUE)
text(.1,m+ .5, round(m,1), cex=1.1,pos=4,offset =1,col="black")}

So my graph would have no points, just the line in m.

Executable code below

library(dplyr)

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


f1 <- 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
  
  
  plot(Numbers ~ Days,  xlim= c(0,45), ylim= c(0,30),
       xaxs='i',data = datas,main = paste0(dmda, "-", CategoryChosse))
  
  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){
    abline(h=m,lwd=2) 
    points(0, m, col = "red", pch = 19, cex = 2, xpd = TRUE)
    text(.1,m+ .5, round(m,1), cex=1.1,pos=4,offset =1,col="black")}
  
  else if(any(table(datas$Numbers) >= 3) & length(unique(datas$Numbers)) == 1){
    yz <- unique(datas$Numbers)
    lines(c(0,datas$Days), c(yz, datas$Numbers), lwd = 2)
    points(0, yz, col = "red", pch = 19, cex = 2, xpd = TRUE)
    text(.1,yz+ .5,round(yz,1), cex=1.1,pos=4,offset =1,col="black")}
  
  else{
    mod <- nls(Numbers ~ b1*Days^2+b2,start = list(b1 = 0,b2 = 0),data = datas, algorithm = "port")
    new.data <- data.frame(Days = with(datas, seq(min(Days),max(Days),len = 45)))
    new.data <- rbind(0, new.data)
    lines(new.data$Days,predict(mod,newdata = new.data),lwd=2)
    coef<-coef(mod)[2]
    points(0, coef, col="red",pch=19,cex = 2,xpd=TRUE)
    text(.99,coef + 1,max(0, round(coef,1)), cex=1.1,pos=4,offset =1,col="black")
  }
}


f1("2021-06-30", "ABC")
f1("2021-07-01", "ABC")
f1("2021-07-02", "ABC")

enter image description here enter image description here

  • Before the `points` is called, you are creating the `plot`. When you have `datas` with 0 rows, the step `plot(Numbers ~ Days, xlim= c(0,45), ylim= c(0,30), xaxs='i',data = datas,main = paste0(dmda, "-", CategoryChosse))` doesn't work and also the error happens before that step – akrun Oct 13 '21 at 20:12
  • Akrun, Is it possible to do an `if`` the same in this question: https://stackoverflow.com/questions/69535009/how-to-tweak-code-to-generate-output-table –  Oct 13 '21 at 20:16
  • I would say that the columns "DR0" are getting dropped for that third case because all the valuesin that column were 0 `SPV <- SPV %>% filter(date2 == dmda, Category == CategoryChosse) %>% select(-any_of(dropnames))`. Probably, we may need a condition here if none of the DRO columns are present – akrun Oct 13 '21 at 20:18
  • If there are no DR0 columns, `summarize(across(starts_with("DR0"), sum))` step will fail. What do you want as value in that case – akrun Oct 13 '21 at 20:22
  • So, I thought like this: if a given date has DR0 columns equal to 0 or has no information, that is, NA, a plot will be made, and the line will be that condition I mentioned. The question is how to generate this plot, if `datas` are giving problems, correct? –  Oct 13 '21 at 20:29
  • I posted a solution. Now, it should plot without any error – akrun Oct 13 '21 at 20:29
  • Can you please check whether that solution would be okay or you need any modification? – akrun Oct 13 '21 at 20:32
  • I'll check now. Just one question, if by chance I have the date in my database, but I don't have information in DR0 columns, that is, NA, can this same condition you made for 0. It possible to do this in the code as well? –  Oct 13 '21 at 20:35
  • Can you try that example on the solution here and if it doesn't work, we can debug – akrun Oct 13 '21 at 20:36

1 Answers1

2

The DR0 columns are removed in the last case and this results in error because the summarise is looping through those columns summarize(across(starts_with("DR0"), sum)). An option is to create a condition check i.e. if there are no DR0 columns left then add those columns as NA and it should work without any error

f1 <- 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(length(grep("DR0", names(SPV))) == 0) {
    SPV[mat1] <- NA_real_
  }
 
  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
  
  
  
  plot(Numbers ~ Days,  xlim= c(0,45), ylim= c(0,30),
       xaxs='i',data = datas,main = paste0(dmda, "-", CategoryChosse))
  
 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){
   abline(h=m,lwd=2) 
   points(0, m, col = "red", pch = 19, cex = 2, xpd = TRUE)
   text(.1,m+ .5, round(m,1), cex=1.1,pos=4,offset =1,col="black")}
 
 else if(any(table(datas$Numbers) >= 3) & length(unique(datas$Numbers)) == 1){
   yz <- unique(datas$Numbers)
   lines(c(0,datas$Days), c(yz, datas$Numbers), lwd = 2)
   points(0, yz, col = "red", pch = 19, cex = 2, xpd = TRUE)
   text(.1,yz+ .5,round(yz,1), cex=1.1,pos=4,offset =1,col="black")}
 
 else{
   mod <- nls(Numbers ~ b1*Days^2+b2,start = list(b1 = 0,b2 = 0),data = datas, algorithm = "port")
   new.data <- data.frame(Days = with(datas, seq(min(Days),max(Days),len = 45)))
   new.data <- rbind(0, new.data)
   lines(new.data$Days,predict(mod,newdata = new.data),lwd=2)
   coef<-coef(mod)[2]
   points(0, coef, col="red",pch=19,cex = 2,xpd=TRUE)
   text(.99,coef + 1,max(0, round(coef,1)), cex=1.1,pos=4,offset =1,col="black")
 }
 
}

-testing

f1("2021-07-02", "ABC")

-output

enter image description here

akrun
  • 874,273
  • 37
  • 540
  • 662
  • Akrun, you insert: ` if(length(grep("DR0", names(SPV))) == 0) { SPV[mat1] <- NA_real_ } ` and what else did you enter just so I know? –  Oct 13 '21 at 20:42
  • @JVieira Just only that change – akrun Oct 13 '21 at 20:44
  • Ah right! And how does he get the `m` line? The result is right, it's just to understand. –  Oct 13 '21 at 20:49
  • 1
    @JVieira that is based on the `df1` i.e. `m<-df1 %>% group_by(Category,Week) %>% summarize(across(starts_with("DR1"), mean))` which is unrelated to datas – akrun Oct 13 '21 at 20:56
  • 1
    then the condition is met when nrow is less than or equal to 2 i..e `m<-subset(m, Week == df1$Week[match(ymd(dmda), ymd(df1$date2))] & Category == CategoryChosse)$DR1 if (nrow(datas)<=2){ abline(h=m,lwd=2) points(0, m, col = "red", pch = 19, cex = 2, xpd = TRUE) text(.1,m+ .5, round(m,1), cex=1.1,pos=4,offset =1,col="black")}` – akrun Oct 13 '21 at 20:56
  • Thank you Akrun! I used it for my larger database. It gave an error when plotting `(Error in plot.window(...) : need finite 'ylim' values)` because in the code I'm using the `mxrange` of this question that you helped me https://stackoverflow.com/questions/69338911/make-selection-adjustments-in-r I think it's better to ask a new question about this, right? –  Oct 13 '21 at 21:12
  • @JVieira I guess the `ylim` you are providing a fixed value right ? `ylim= c(0,30)`? – akrun Oct 13 '21 at 21:14
  • @JVieira it is possible that there are NA elements. can you try `mxrange <- range(datas$Numbers, na.rm = TRUE)`. If there are `Inf`inite values, then it may needs to be taken care off – akrun Oct 13 '21 at 21:15
  • Akrun, I decided to ask a new question to not prolong it any longer here: https://stackoverflow.com/questions/69562487/error-in-plot-window-need-finite-ylim-values-to-generate-graph-in-r –  Oct 13 '21 at 21:25
  • Akun, thanks for the answer in the other question! I was thinking regarding this question here. I inserted a few DR0 in database df1 and it works fine. However, I'm using it for a case where it has 45 DR0. So when I do `datas`, it gives a big number, so it uses `nls` function instead of using that condition that uses the line `m`. Now that I think about it. Any tips on what to do? –  Oct 13 '21 at 22:08
  • 1
    @JVieira May be you can subset the number of DR0 if the length is greater than say 10 or so i..e you may select with `SPV[head(mat1, 10)] <- NA_real_` – akrun Oct 13 '21 at 22:10
  • Maybe I am not ffollowing the part you mentioned. Is it to create the `SPV` DR0 columns when they don't exist – akrun Oct 13 '21 at 22:12
  • 1
    Akun, I used `SPV[head(mat1, 10)] <- NA_real` and it was good for my case. Now I think there's is just need to adjust this question of the DR0 columns have no values, that is, NA. I can ask a new question if you want –  Oct 13 '21 at 23:33
  • @JVieira I think `values_drop_na = TRUE` can be added in `pivot_longer` if you want to remove those NA elements while reshaping – akrun Oct 13 '21 at 23:36
  • `pivot_longer`of `mat1`or `datas`´? –  Oct 13 '21 at 23:39
  • In general, when you are using `pivot_longer`. In both case, if there are NAs, those rows will be kept as the default argument is `values_drop_na = FALSE` – akrun Oct 13 '21 at 23:40
  • I am going offline. – akrun Oct 13 '21 at 23:46