簡體   English   中英

如何在 R 中插入新條件到我的 function

[英]How to insert new condition in my function in R

你能幫我在下面的 function 中再插入一個條件嗎? 到目前為止,我有兩個條件: (nrow(datas)<=2) ,創建的行將使用abline function。如果它更大,我將使用mod function 從那里創建我的行。 這對我來說可以。 但是我想創建另一個條件,即如果datas呈現 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")

你能幫我在我的function中調整一下嗎?

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)])-2):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{
    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")
    
  }
}

可變datas

<-datas
  Category Days Numbers
  <chr>    <dbl>   <dbl>
1 ABC       42       2
2 ABC       43       2
3 ABC       44       2

它對這個不起作用。

<-datas
  Category Days Numbers
  <chr>    <dbl>   <dbl>
1 ABC       41       10.0
1 ABC       42       10.0
2 ABC       43       10.0
3 ABC       44       10.5

聽起來你只想要一個else if

...

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

編輯:

> datas <- data.frame(a = c(1,1,1), b = c(2,2,2))
> datas
  a b
1 1 2
2 1 2
3 1 2
# Test if each row is a duplicate
> duplicated(datas)
[1] FALSE  TRUE  TRUE
> sum(TRUE)
[1] 1
> sum(duplicated(datas)) >= 2
[1] TRUE

# But beware, it is counting all duplicates, not
# just for a specific duplicate existing three times.
# For example:

> datas <-data.frame(a = c(3, 3, 4, 4, 5, 5), b = c(3, 3, 4, 4, 5, 5))
> datas
  a b
1 3 3
2 3 3
3 4 4
4 4 4
5 5 5
6 5 5
> duplicated(datas)
[1] FALSE  TRUE FALSE  TRUE FALSE  TRUE
> sum(duplicated(datas))
[1] 3

# This is more straightforward if you are
# just interested in duplicates in one column:

# This is the frequency of each value in datas$b
> table(datas$b)
3 4 5 
2 2 2 
# Which frequencies greater than or equal to 3?
> table(datas$b) >= 3
3     4     5 
FALSE FALSE FALSE
# Are ANY frequencies greater than or equal to 3?
> any(table(datas$b)>=3)
[1] FALSE
# You could try this:
# }else if(any(table(datas$Numbers) >= 3)){...new function...}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM