[R] Help with cumsum function

Uwe Ligges ligges at statistik.uni-dortmund.de
Sat Aug 11 18:31:21 CEST 2007



Hectorman Hectorman wrote:
> Hello!
> 
> I have a question regarding the cumsum function that I do not know how to 
> solve. Would appreciate help from someone. I have imported data from a 
> txtfile with 2 columns. I am interested in the seconds column, which 
> contains numbers from i=0 to 40. I would like to count the number of 
> instances of the numbers 0 to 40 in the seconds column. My problem is that 
> if there are no observations of one number i in the second column cumsum 
> skips this in the output.
> 
> Below is an example of the output of the argument  Y=cumsum(table(x[,2]))
>   0   1   2   3   5     6   7    8    9  10  11  12  13  14  15  16  17  18  
> 19   20   21   22   23   24
>   3   5   6  10  13  14  16  18  21  25  27  30  32  35  38  40  42  54 105 
> 233 306 341 383 417
> 25    26  27  28  29    30   31   32  33  34   35   36   37  38   39   40
> 441 468 487 502 518 532 542 546 552 564 566 574 578 584 591 594
> 
> As you can see from the output there are no observations of i=4 in this 
> column. Are there any way I could return the following result instead
> 0   1   2   3   4    5     6   7    8      and so on
> 3   5  6  10  10 13   14  16  18
> 
> When i=4 cumsum is the same as when i=3 (10)
> 


Y <- cumsum(table(factor(x[,2], levels=0:40)))

Uwe Ligges


> I would really appreciate if anyone could help me with this one:)
> 
> Jan Moberg
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list