例如,我有以下代码:
cumsum(1:100)
如果元素i + 1大于,我想打破它3000
。我怎样才能做到这一点?
所以代替这个结果:
[1] 1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 210 231 253 276 300
[25] 325 351 378 406 435 465 496 528 561 595 630 666 703 741 780 820 861 903 946 990 1035 1081 1128 1176
[49] 1225 1275 1326 1378 1431 1485 1540 1596 1653 1711 1770 1830 1891 1953 2016 2080 2145 2211 2278 2346 2415 2485 2556 2628
[73] 2701 2775 2850 2926 3003 3081 3160 3240 3321 3403 3486 3570 3655 3741 3828 3916 4005 4095 4186 4278 4371 4465 4560 4656
[97] 4753 4851 4950 5050
我想得到以下结果:
[1] 1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 210 231 253 276 300
[25] 325 351 378 406 435 465 496 528 561 595 630 666 703 741 780 820 861 903 946 990 1035 1081 1128 1176
[49] 1225 1275 1326 1378 1431 1485 1540 1596 1653 1711 1770 1830 1891 1953 2016 2080 2145 2211 2278 2346 2415 2485 2556 2628
[73] 2701 2775 2850 2926
正如我在评论中提到的那样,即使对于像我这样的人,用Rcpp编写简单的内容也没什么大不了的。这是一个看起来很原始的实现(感谢@ MatthewLundberg的改进建议)
library(Rcpp)
cppFunction('NumericVector cumsumCPP(NumericVector x, int y = 0){
// y = 0 is the default
// Need to do this in order to avoid modifying the original x
int n = x.size();
NumericVector res(n);
res[0] = x[0];
for (int i = 1 ; i < n ; i++) {
res[i] = res[i - 1] + x[i];
if (res[i] > y && (y != 0)) {
// This breaks the loop if condition met
return res[seq(0, i - 1)];
}
}
// This handles cases when y== 0 OR y != 0 and y > cumsum(res)
return res;
}')
cumsumCPP(1:100, 3000)
# [1] 1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 210 231 253 276 300
# [25] 325 351 378 406 435 465 496 528 561 595 630 666 703 741 780 820 861 903 946 990 1035 1081 1128 1176
# [49] 1225 1275 1326 1378 1431 1485 1540 1596 1653 1711 1770 1830 1891 1953 2016 2080 2145 2211 2278 2346 2415 2485 2556 2628
# [73] 2701 2775 2850 2926
与基数Rs相似cumsum
,这对整数和浮点数均有效,并且不处理NA
s。阈值的默认值设置为0
-如果要限制负值cumsum
,则不理想,但是我暂时还没有想到更好的值(您可以自己决定一个值)。
虽然可以使用一些优化...
set.seed(123)
x <- as.numeric(sample(1:1e3, 1e7, replace = TRUE))
microbenchmark::microbenchmark(cumsum(x), cumsumCPP(x))
# Unit: milliseconds
# expr min lq mean median uq max neval cld
# cumsum(x) 58.61942 61.46836 72.50915 76.7568 80.97435 99.01264 100 a
# cumsumCPP(x) 98.44499 100.09979 110.45626 112.1552 119.22958 131.97619 100 b
identical(cumsum(x), cumsumCPP(x))
## [1] TRUE
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句