Graph is not generated if I consider the else if condition

Could you help me understand why I can generate the graph when my else if condition is not being considered, but when I consider, the graph is not generated? I found it strange that this happens, any help is welcome! If you can help me fix this, I appreciate it!

Note: My idea is that if datas$Numbers has 3 or more identical values it executes the else if condition. Otherwise, the else condition, which uses the nls function. As can be seen datas has 4 numbers, being 3 equal (10) and one different (10.5), so the correct function for this case is nls function.

Not considering else if

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,2,0,3,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,1,4,5),
       DR08 = c(3,4,1,4,4),DR09 = c(2,3,1,4,5),DR10 = c(2,3,1,4,5),DR11 = c(2,3,0,4,5),DR12 = c(2,3,0,4,5)),
  class = "data.frame", row.names = c(NA, -5L))


datas<-structure(list(Category = c("ABC", "ABC", "ABC", "ABC"), 
                      Days = c(41, 42, 43, 44), 
                      Numbers = c(10,10, 10, 10.5)), row.names = c(NA, -4L), class = "data.frame")

f1 <- function(dmda, CategoryChosse) {
  
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)){
  #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-29", "ABC")

enter image description here

Considering else if

f1 <- function(dmda, CategoryChosse) {
  
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)){
  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-29", "ABC")
 Error in xy.coords(x, y) : 'x' and 'y' lengths differ 

Is the issue here? Differing lengths.

lines(c(0,datas$Days), c(yz, datas$Numbers), lwd = 2)


Browse[1]> c(0,datas$Days)
[1]  0 41 42 43 44
Browse[1]> c(yz, datas$Numbers)
[1] 10.0 10.5 10.0 10.0 10.0 10.5

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.