English 中文(简体)
• 如何设立新栏,将现有一栏的累计总额加起来?
原标题:How to create new column that cumulatively sums to cumulative sum of existing column?

借助逆向功能,Im 想建立一个新的一栏数据,该一栏总和在第一栏中计算,但使用不超过<代码>incr的加权。

That could start with df

incr <- 1.5
df <- data.frame(a = c(6,0,0,2.5,0,0))
df
    a
1 6.0
2 0.0
3 0.0
4 2.5
5 0.0
6 0.0

then create a new column b using incr:

    a   b
1 6.0 1.5
2 0.0 1.5
3 0.0 1.5
4 2.5 1.5
5 0.0 1.5
6 0.0 1.0

我发现<代码>tidyr:uncount(),这似乎是一种可以发挥作用的职能,但我需要使用非强迫加薪。 通常试图改变和使用病媒功能,并想将其操作起来<>代码>rowwise(),但我的大多数想法都需要加以利用。

From comment below: If analogies are helpful, think of it like queued downloads. Column a shows you press to download 6 MB at time 1, 0 MB for times 2 and 3, then you press to download 2.5 MB at time 4. Your connection, however, can only download at the speed of incr. So, if incr is 1.5, column b shows what actually downloaded. You fully use that connection speed each period until you download the final residual (1.0) in time 6.

为了更好地强调层面,这里还有一点:

incr <- 1.5
df <- data.frame(a = rep(0,100),b=rep(0,100))
df$a[c(30,33,38)] = c(6,2.5,1)
df[30:39,]
     a b
30 6.0 0
31 0.0 0
32 0.0 0
33 2.5 0
34 0.0 0
35 0.0 0
36 0.0 0
37 0.0 0
38 1.0 0
39 0.0 0

具有预期产出

     a   b
30 6.0 1.5
31 0.0 1.5
32 0.0 1.5
33 2.5 1.5
34 0.0 1.5
35 0.0 1.0
36 0.0 0.0
37 0.0 0.0
38 1.0 1.0
39 0.0 0.0
最佳回答

由于我不知道何时放弃,我认为唯一的解决办法是放弃。 但我认为,你总是能够使C++成为超高速的病媒功能:

Rcpp::cppFunction(
  "
    NumericVector iterate_to_cumsum(NumericVector v1, double incr) {
    int x = v1.size();
    NumericVector v2(x);

    for (int i =0; i < x; ++i) {
      v2[i] = std::max(0.0, std::min(incr, sum(v1[Rcpp::Range(0, i)]) - sum(v2)));
    }

    return v2;

  }
"
)

library(tidyverse)

df <- data.frame(a = c(6, 0, 0, 2.5, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0))
incr <- 1.5

df |> 
  mutate(b = iterate_to_cumsum(a, incr))
#>      a   b
#> 1  6.0 1.5
#> 2  0.0 1.5
#> 3  0.0 1.5
#> 4  2.5 1.5
#> 5  0.0 1.5
#> 6  0.0 1.0
#> 7  0.0 0.0
#> 8  0.0 0.0
#> 9  1.0 1.0
#> 10 0.0 0.0
#> 11 3.0 1.5
#> 12 0.0 1.5
#> 13 0.0 0.0
#> 14 0.0 0.0
问题回答

页: 1 备有<代码>if的序号:

Set up dataframe

incr <- 1.5
df <- data.frame(a = rep(0,100),b=rep(0,100))
df$a[c(30,33,38)] = c(6,2.5,1)
df <- df[29:39,]
df
     a b
29 0.0 0 # note I included first row as 0 to test
30 6.0 0
31 0.0 0
32 0.0 0
33 2.5 0
34 0.0 0
35 0.0 0
36 0.0 0
37 0.0 0
38 1.0 0
39 0.0 0

for (i in 1:nrow(df)) {
  if (i == 1 & df$a[[1]] < incr) {
    df$b[i] <- df$a[[1]]
  } else if (sum(df$a[1:i]) - sum(df$b[1:i]) > incr) {
    df$b[i] <- incr
  } else {
    df$b[i] <- sum(df$a[1:i]) - sum(df$b[1:i]) 
  } 
}

Output

df
     a   b
29 0.0 0.0
30 6.0 1.5
31 0.0 1.5
32 0.0 1.5
33 2.5 1.5
34 0.0 1.5
35 0.0 1.0
36 0.0 0.0
37 0.0 0.0
38 1.0 1.0
39 0.0 0.0




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签