[R] sum sections of data of different lengths from within a data frame

jim holtman jholtman at gmail.com
Wed Feb 10 03:30:27 CET 2010


WIll this do it for you:

> x <- read.table(textConnection("ColA  ColB
+ 1        0
+ 3        0
+ 2        1
+ 2        0
+ 1        0
+ 4        0
+ 1        1
+ 9        1
+ 3        0
+ 5        0
+ 2        1"), header=TRUE)
> closeAllConnections()
> x.s <- split(x, cumsum(x$ColB))
> x.l <- do.call(rbind, lapply(x.s, function(.grp){
+     newdata <- cbind(.grp, sum=cumsum((.grp$ColB == 0) * .grp$ColA))
+     newdata$sum[newdata$ColB == 1] <- .001
+     newdata
+ }))
>
> x.l
     ColA ColB   sum
0.1     1    0 1.000
0.2     3    0 4.000
1.3     2    1 0.001
1.4     2    0 2.000
1.5     1    0 3.000
1.6     4    0 7.000
2       1    1 0.001
3.8     9    1 0.001
3.9     3    0 3.000
3.10    5    0 8.000
4       2    1 0.001
>


On Tue, Feb 9, 2010 at 9:06 PM, Kara Przeczek <przeczek at unbc.ca> wrote:
> Dear R Help:
>
> I am trying to sum data from one column in a dataframe based on a value in another. I do not know how to do this easily in R.
> For example:
>
> Col A  Col B
> 1        0
> 3        0
> 2        1
> 2        0
> 1        0
> 4        0
> 1        1
> 9        1
> 3        0
> 5        0
> 2        1
>
> I would like to cumsum the values in Col A for all rows where Col B is 0, and a value of 1 in Col B will reset the sum and give a value of 0.001. Thus, for this table I would like an output of 1, 4, 0.001, 2, 3, 7, 0.001, 0.001, 3, 8, 0.001.
> I tried using a For loop, but that summed all the Col A values together. I need something that does
> For (i in 1:length(df$Col B))
> {
> IF{Col B == 0, cumsum(Col A) "until" Col B == 1, else 0.001}
> }
> I don't know how to use "until" in R.
> Any help would be greatly appreciated!
> Kara
>
> ______________________________________________
> R-help at r-project.org 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list