【问题标题】:Graph is not generated if I consider the else if condition如果我考虑 else if 条件,则不会生成图表
【发布时间】:2021-11-30 14:04:36
【问题描述】:

你能帮我理解为什么我可以在不考虑我的else if 条件时生成图表,但是当我考虑时,图表没有生成?我发现发生这种情况很奇怪,欢迎任何帮助!如果您能帮我解决这个问题,我将不胜感激!

不考虑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")

考虑到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 

【问题讨论】:

  • 这一行 yz &lt;- unique(datas$Numbers) 找到两个唯一值,10 和 10.5。这意味着在下一行中,当您调用 lines() 时,您的 x 和 y 参数的长度不同。
  • 感谢乔兰的回复!我该如何解决这个问题?我的想法是,如果 datas$Numbers 有 3 个或更多相同的值,它会执行 else if 条件。否则,elsecondition,即nls 函数。可以看出datas有4个数字,3个相等(10)和1个不同(10.5),所以这种情况下的正确函数是nls
  • @Jose,@JViera 让我看看这个问题。我认为问题在于理解您在if()else if() 语句中使用的标准。你能用文字写出你想在else if()中发生的事情吗?我怀疑您可能想要检测datas$Numbers 包含三个相同值而没有别的情况。在这种情况下,这将起作用:}else if(any(table(datas$Numbers) &gt;= 3) &amp; length(unique(datas$Numbers)) == 1){
  • Skaqqs,你说得对!我的逻辑错了。您要插入作为答案吗?

标签: r plot


【解决方案1】:

这个答案与我的另一个答案 (see here) 密切相关,后者是对一个非常相似的问题的回答。

...

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")
  
  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")
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多