简体   繁体   English

R 循环直到条件匹配,然后 go on

[英]R loop until condition matched, then go on

I have a dataframe with numerical values in one row.我有一个 dataframe,一行中有数值。 Now I want to calculate the cumsum of those rows, until >= 1. If this point is reached -> print for all those rows a counter, write in every row the cumsum for its counter, then look for the cumsum of the next rows.现在我想计算这些行的 cumsum,直到 >= 1。如果达到这一点 -> 为所有这些行打印一个计数器,在每一行中写入其计数器的 cumsum,然后查找下一行的 cumsum .

Should look somewhow like this:应该看起来像这样:

value    counter   cumsum
0.3      1         0.9
0.3      1         0.9
0.3      1         0.9
0.3      2         0.4
0.1      2         0.4
2        3         2

My problem is how to tell R to stop the cumsum, if >= than 1. Any ideas?我的问题是如果 >= than 1,如何告诉 R 停止 cumsum。有什么想法吗? Thank you in advance.先感谢您。

I don't know if I understood your problem correctly, but maybe this one here helps:我不知道我是否正确理解了你的问题,但也许这里有帮助:

value = round(runif(20, min = 0.1, max = 0.5), 1)

csumVec = numeric(length(value))
counterVec = numeric(length(value))
startIndex = 1
csum = 0
counter = 1

for(i in 1:length(value)) {
  csum = csum + value[i]
  if(csum > 1) {
    counterVec[startIndex:i] = counter
    csumVec[startIndex:i] = csum-value[i]
    startIndex = i
    counter = counter+1
    csum = value[i]
  }
  if(i == length(value)) {
    counterVec[startIndex:i] = counter
    csumVec[startIndex:i] = csum
  }
}

cbind(value, counterVec, csumVec)

It seems like you can calculate the cumulative sum, divide by 1, and take the floor() (round down)好像可以计算累计和,除以1,取floor() (向下舍入)

floor(cumsum(value) / 1)
## [1] 0 0 0 1 1 3

This is correct, except that it is 0-based and the counter does not increment by 1. Fix these by matching the result above with their unique values这是正确的,除了它是从 0 开始的并且counter不会递增 1。通过将上面的结果与其唯一值匹配来解决这些问题

counter0 = floor(cumsum(value) / 1)
counter = match(counter0, unique(counter0))
counter
## [1] 1 1 1 2 2 3

Having got the 'tricky' part, I'd use dplyr ( library(dplyr) ) for the rest有了“棘手”的部分,我将使用 dplyr ( library(dplyr) ) 作为 rest

## library(dplyr)
tibble(value, counter) |>
    mutate(cum_sum = cumsum(value)) |>
    group_by(counter) |>
    mutate(cumsum = max(cumsum(value)))
## # A tibble: 6 × 3
## # Groups:   counter [3]
##   value counter cumsum
##   <dbl>   <int>  <dbl>
## 1   0.3       1    0.9
## 2   0.3       1    0.9
## 3   0.3       1    0.9
## 4   0.3       2    0.4
## 5   0.1       2    0.4
## 6   2         3    2

or perhaps capturing the tricky part in a (more general) function或者也许在(更一般的)function 中捕获棘手的部分

cumgroup <- function(x, upper = 1) {
    counter0 = floor(cumsum(x) / upper)
    match(counter0, unique(counter0))
}

and incorporating into the dplyr solution并纳入 dplyr 解决方案

tibble(value) |>
    mutate(counter = cumgroup(value)) |>
    group_by(counter) |>
    mutate(cumsum = max(cumsum(value)))

or depending on what precisely you want或者取决于你想要什么

tibble(value) |>
    mutate(
        cumsum = cumsum(value) %% 1,
        counter = cumgroup(value)
    ) |>
    group_by(counter) |> 
    mutate(cumsum = max(cumsum)) |>
    select(value, counter, cumsum)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM