search for: cumsum

Displaying 20 results from an estimated 921 matches for "cumsum".

2009 Nov 11
2
partial cumsum
Hello, I am searching for a function to calculate "partial" cumsums. For example it should calculate the cumulative sums until a NA appears, and restart the cumsum calculation after the NA. this: x <- c(1, 2, 3, NA, 5, 6, 7, 8, 9, 10) should become this: 1 3 6 NA 5 11 18 26 35 45 any ideas? thank you and best regards, stefan
2012 Nov 22
4
Using cumsum with 'group by' ?
...ooks like this: id time x 1 12:01 5 1 12:02 14 1 12:03 6 1 12:04 3 2 12:01 98 2 12:02 23 2 12:03 1 2 12:04 4 3 12:01 5 3 12:02 65 3 12:03 23 3 12:04 23 But I want to add a column which is the cumulative sum of X, but only by id. I've used cumsum before, but not in this way. So the result should be something like: id time x cumsum 1 12:01 5 5 1 12:02 14 19 1 12:03 6 25 1 12:04 3 28 2 12:01 98 98 2 12:02 23 121 2 12...
2012 Feb 14
1
cumsum function to determine plankton phenology
...ant to obtain the start, middle and end dates of cumulative abundance (for those ecologically minded folks the reference is in a Greve, et al., 2005 paper) to determine phenology of a species or functional group. I previously applied this method in Excel and want to replicate the formula in R using cumsum. Easy right? Excel method: 1. calculate cumulative sum 2. divide the cumulative sum for each time point by the annual cumulative sum or maximum value 3. Calculate three different percentile thresholds corresponding to the start (when a population reaches 15% of cumulative annual abundance), the mi...
2011 Nov 26
5
cumsum in 3d arrays
Hello! Is it posible to apply /cumsum()/ along the 3rd dimension of 3D array? Something like matrlab function - /cumsum (*A*,dim)/ which returns the cumulative sum of the elements along the dimension of *A* specified by scalar dim. Thanks in advance ?eljka -- View this message in context: http://r.789695.n4.nabble.com/cumsum-in-...
2020 Aug 23
2
sum() vs cumsum() implicit type coercion
Hi I noticed a small inconsistency when using sum() vs cumsum() I have a char-based series > tryjpy$long [1] "0.0022" "-0.0002" "-0.0149" "-0.0023" "-0.0342" "-0.0245" "-0.0022" [8] "0.0003" "-0.0001" "-0.0004" "-0.0036" "-0.001&quot...
2020 Aug 25
1
sum() vs cumsum() implicit type coercion
>>>>> Tomas Kalibera >>>>> on Tue, 25 Aug 2020 09:29:05 +0200 writes: > On 8/23/20 5:02 PM, Rory Winston wrote: >> Hi >> >> I noticed a small inconsistency when using sum() vs cumsum() >> >> I have a char-based series >> >> > tryjpy$long >> >> [1] "0.0022" "-0.0002" "-0.0149" "-0.0023" "-0.0342" "-0.0245" "-0.0022" >> >> [...
2008 Jun 18
4
inverse cumsum
...2 0.72 0.50 1.29 0.54 1.22 1992 2.15 1.28 1.23 2.26 1.22 3.17 1993 1.50 0.87 1.68 1.97 0.83 2.55 1994 0.69 0.00 0.76 1.89 0.60 0.87 1995 1.13 1.04 1.19 1.52 1.13 1.78 Can I utilise a cumsum inverse? from 1995 to 1985? Thank you, Alfredo
2009 Nov 21
2
how to ignore NA when using cumsum?
I would like to cumulatively sum rows in a matrix, in which each row has 1 NA value. The usual "na.rm=TRUE" does not seem to work with the command cumsum. Is there another way to ignore the NAs or do I need to figure out a different way to do this? Here's an example matrix of title "proportion": Ntrail Strail NFJD Baldy Onion Crane [1,] NA 0.2944937 0.1779969 0.1808015 0.2296511 0.11705683 [2,] 0....
2009 Feb 17
2
cumsum vs. sum
I recently traced a bug of mine to the fact that cumsum(s)[length(s)] is not always exactly equal to sum(s). For example, x<-1/(12:14) sum(x) - cumsum(x)[3] => 2.8e-17 Floating-point addition is of course not exact, and in particular is not associative, so there are various possible reasons for this. Perhaps sum uses clever summing t...
2005 May 30
2
trouble with cumsum?
Dear R users, I am using R version 2.0.1 (2004/11/15) on an i386-pc-mingw32 platform. I encounter the following problem while using cumsum: > a <- rep(0.01, 100) > b <- cumsum(a) > sum(a) == 1 [1] TRUE > b[100] == 1 [1] FALSE Am I missing something? Should cumsum have such an outcome? Thanks in advance for any clarifications any of you can offer. Regards, Makram Talih -- Makram Talih, Ph.D. Assistant Professor...
2002 Jul 24
4
cumsum and subsets of a data frame?
I have a question about using cumsum on subsets of a data frame. Suppose I have a frame that looks something like this > tmp f x y 1 left 1 0 2 left 2 0 3 left 3 9 4 left 4 10 5 left 5 23 6 left 6 45 7 left 7 13 8 left 8 2 9 left 9 6 10 right 1 10 11 right 2 26 12 right 3 9 13 right 4 50 14 right 5...
2011 May 06
1
Cumsum in Lattice Panel Function
I'm trying to create an xyplot with a "groups" argument where the y-variable is the cumsum of the values stored in the input data frame. I almost have it, but I can't get it to automatically adjust the y-axis scale. How do I get the y-axis to automatically scale as it would have if the cumsum values had been stored in the data frame? Here is the code I have so far: require(lattice)...
2009 Nov 21
1
how to ignore NA when using cumsum WHILE retaining NAs?
I would like to cumulatively sum rows in a matrix, in which each row has 1 NA value, which I do NOT want to treat as 0s. The NAs are placeholders where there is actually no data, which is not the same as a 0. The usual "na.rm=TRUE" does not seem to work with the command cumsum. Is there another way to ignore the NAs or do I need to figure out a different way to do this? Here's an example matrix of title "proportion": Ntrail Strail NFJD Baldy Onion Crane [1,] NA 0.2944937 0.1779969 0.1808015 0.2296511 0.11705683 [2,] 0....
2010 Oct 12
5
aggregate with cumsum
Hello everybody, Data is myd <- data.frame(id1=rep(c("a","b","c"),each=3),id2=rep(1:3,3),val=rnorm(9)) I want to get a cumulative sum over each of id1. trying aggregate does not work myd$pcum <- aggregate(myd[,c("val")],list(orig=myd$id1),cumsum) Please suggest a solution. In real the dataframe is huge so looping with for and subsetting is not a great idea (still doable, though). Thank you Stephen B [[alternative HTML version deleted]]
2009 Jul 01
1
running count in data.frame
...had an example of doing this for random coin flips and I tried to modify it. It seems to sort of work in the beginning, but then it stops and I don't understand why. I'm trying to duplicate essentially the Excel capability of =SUM($A$1:$A(Row number)) The example looked like this: x = cumsum(sample(c(-1,1),100,replace=T)) which does seem to work: (100 shortened to 20 for email) > cumsum(sample(c(-1,1),20,replace=T)) [1] 1 0 1 0 1 2 3 4 5 4 3 4 5 6 7 6 5 4 5 6 > cumsum(sample(c(-1,1),20,replace=T)) [1] 1 2 1 2 1 2 3 2 3 4 5 6 7 8 7 8 7 8 9 8 > cumsum(sample(c(-1,1),20,repla...
2002 Dec 05
2
Problems with segments and multiple graphs
I would like to create a page of two graphs (2 rows by 1 col) and then draw vertical lines (segments?) on both graphs from the minimum values to the corresponding maximum value. So I have tried # > y <- rnorm(3000) > par(mfrow=c(2,1)) > plot(y,type="l") > plot(cumsum(y),type="l") > segments(1000,min(cumsum(y)),1000,max(cumsum(y))) > par(mfg=c(1,1)) > segments(1000,min(y),1000,max(y)) > y <- rnorm(3000) The segment looks fine on the bottom graph but I get a small vertical line on the top graph. The max(y) value looks OK but the min(y)...
2011 Dec 09
3
bug in sum() on integer vector
Hi, x <- c(rep(1800000003L, 10000000), -rep(1200000002L, 15000000)) This is correct: > sum(as.double(x)) [1] 0 This is not: > sum(x) [1] 4996000 Returning NA (with a warning) would also be acceptable for the latter. That would make it consistent with cumsum(x): > cumsum(x)[length(x)] [1] NA Warning message: Integer overflow in 'cumsum'; use 'cumsum(as.numeric(.))' Thanks! H. > sessionInfo() R version 2.14.0 (2011-10-31) Platform: x86_64-unknown-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_CA.UTF-8 LC_NUMERI...
2007 Nov 03
2
cumsum
Hi, my problem belongs to the basic ones. I want to get cumulated sum over the matrix columns by one command (if such exists). Ordinary R's cumsum(x) when x is: [,1] [,2] [1,] 1 5 [2,] 2 6 [3,] 3 7 [4,] 4 8 yields: 1 3 6 10 15 21 28 36 I want: [,1] [,2] [1,] 1 5 [2,] 3 11 [3,] 6 18 [4,] 10 26 Is there any command to do so?? best, robert -- View this message in context: http://www....
2016 Mar 30
2
Compute the Gini coefficient
...r of parasites per host: parasites = c(0,1,2,3,4,5,6,7,8,9,10) Number of hosts associated with each number of parasites given above: hosts = c(18,20,28,19,16,10,3,1,0,0,0) To represent the Lorenz curve: I manually calculated the cumulative percentage of parasites and hosts: cumul_parasites <- cumsum(parasites)/max(cumsum(parasites)) cumul_hosts <- cumsum(hosts)/max(cumsum(hosts)) plot(cumul_hosts, cumul_parasites, type= "l") >From this Lorenz curve, how can I calculate the Gini coefficient with the function "gini" in R (package reldist) given that the vector "ho...
2017 Jan 20
1
NaN behavior of cumsum
Hi! I noticed that cumsum behaves different than the other cumulative functions wrt. NaN values: > values <- c(1,2,NaN,1) > for ( f in c(cumsum, cumprod, cummin, cummax)) print(f(values)) [1] 1 3 NA NA [1] 1 2 NaN NaN [1] 1 1 NaN NaN [1] 1 2 NaN NaN The reason is that cumsum (in cum.c:33) contains an...