Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用多个顺序访问的阈值重置的累积和_R_Dplyr_Purrr - Fatal编程技术网

R 使用多个顺序访问的阈值重置的累积和

R 使用多个顺序访问的阈值重置的累积和,r,dplyr,purrr,R,Dplyr,Purrr,这个问题类似于,它询问了一种基于阈值重置累积总和的方法。对这个问题的公认答案是一个应用固定阈值重置累积的函数 library(tidyverse) sum_reset_at <- function(thresh) { function(x) { accumulate(x, ~if_else(.x >= thresh, .y, .x + .y)) } } df <- tibble(a = c(2, 3, 1, 2, 2, 3)) df %&g

这个问题类似于,它询问了一种基于阈值重置累积总和的方法。对这个问题的公认答案是一个应用固定阈值重置累积的函数

library(tidyverse)

sum_reset_at <- function(thresh) {
    function(x) {
        accumulate(x, ~if_else(.x >= thresh, .y, .x + .y))
    }
}

df <- tibble(a = c(2, 3, 1, 2, 2, 3))

df %>% mutate(c = sum_reset_at(5)(a))

## # A tibble: 6 x 2
##       a     c
##   <dbl> <dbl>
## 1     2     2
## 2     3     5
## 3     1     1
## 4     2     3
## 5     2     5
## 6     3     3
载体将按要求回收

我在函数中使用了
sample

set.seed(0)

sum_reset_at <- function(thresh) {
    function(x) {
        accumulate(x, ~if_else(.x >= sample(thresh, size = 1), .y, .x + .y))
    }
}

thresholds <- c(5, 3, 2)

df %>% mutate(c = sum_reset_at(thresholds)(a))

## # A tibble: 6 x 2
##       a     c
##   <dbl> <dbl>
## 1     2     2
## 2     3     3
## 3     1     4
## 4     2     2
## 5     2     4
## 6     3     3
set.seed(0)
总和重置=样本(阈值,大小=1),.y,.x+.y))
}
}
阈值%变异(c=在(阈值)(a)处的总和重置)
###tibble:6 x 2
##a c
##    
## 1     2     2
## 2     3     3
## 3     1     4
## 4     2     2
## 5     2     4
## 6     3     3

但我不想随机采样阈值,我想按顺序采样。

您可以在处修改
总和重置\u,以接受thres的向量

sum_reset_at <- function(thresh)
  {
    function(x) {
      i <- 1
      accumulate(x, function(.x, .y) {
        if(.x >= thresh[i])
        {
          #Increment i and return .y
          i <<- i+1
          if (i > length(thresh)) i <<- 1
          .y
        }
        else
        {
          .x + .y
        }
      })
    }
 }

df <- tibble(a = c(2, 3, 1, 2, 2, 3))

df %>% mutate(c = sum_reset_at(c(5,3,1))(a))
## A tibble: 6 x 2
#      a     c
#  <dbl> <dbl>
#1     2     2
#2     3     5
#3     1     1
#4     2     3
#5     2     5
#6     3     3

sum\u reset\u,谢谢@Marcelo。这对我来说非常有效。我从来没见过这张照片
sum_reset_at <- function(thresh)
  {
    function(x) {
      i <- 1
      accumulate(x, function(.x, .y) {
        if(.x >= thresh[i])
        {
          #Increment i and return .y
          i <<- i+1
          if (i > length(thresh)) i <<- 1
          .y
        }
        else
        {
          .x + .y
        }
      })
    }
 }

df <- tibble(a = c(2, 3, 1, 2, 2, 3))

df %>% mutate(c = sum_reset_at(c(5,3,1))(a))
## A tibble: 6 x 2
#      a     c
#  <dbl> <dbl>
#1     2     2
#2     3     5
#3     1     1
#4     2     3
#5     2     5
#6     3     3