[R] cumsum() by subgroup

Don MacQueen macq at llnl.gov
Fri Apr 2 23:51:35 CEST 2004


If you first look at the output of the inner core of your expression:

      split(d.1, d.1$v1)

you might get a hint. Then make this change.

cbind(d.1, v4=c(lapply(split(d.1, d.1$v1), function(x) cumsum(x$v3)), 
recursive=T))

There is no variable named "y" in your data frame, so "x$y" is the 
wrong thing to use in the function definition.

You didn't say which variable you wanted the cumsum of, but v3 seemed 
to make sense...

-Don

At 3:28 PM -0600 4/2/04, Jose A. Hernandez wrote:
>I need to do a simple cumulative sum by group and add the result to the
>data. I found an earlier thread in the help files with a few suggestions.
>
>Somewhat, one of the suggestions does not work with "my data", and I don't
>really understand why ?
>
>The error am getting using the "my data" below is...
>
>Error in data.frame(..., check.names = FALSE) :
>          arguments imply differing number of rows: 10, 0
>
>I'd truly appreciate your input on this matter.
>
>Am still a beginner on R, but am determined to use it for the rest of my
>life...
>
>Thanks in advance.
>
>Sincerely,
>
>Jose
>
>
># The example I found on the help files provided by J. Fox on Wed Jul 24 2002
>f <- c("left","left","left","left","left","left","left","left","left",
>"right","right","right","right","right","right","right","right","right",)
>x <- c(1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9)
>y <- c(0,0,9,10,23,45,13,2,6,10,26,9,50,78,20,7,20,19)
>tmp <- data.frame(f,x,y)
>
>cbind(tmp, s=c(lapply(split(tmp, tmp$f), function(x) cumsum(x$y)),
>recursive=T))
>
>
># my data, the same code but it doesn't work...
>v1 <- c(1,1,1,1,1,2,2,2,2,2)
>v2 <- c(1,2,3,4,5,1,2,3,4,5)
>v3 <- c(0,0.1,0.11,0.3,0.5,0,0.4,0.5,2.4,2.6)
>d.1 <- data.frame(v1,v2,v3)
>
>cbind(d.1, v4=c(lapply(split(d.1, d.1$v1), function(x) cumsum(x$y)),
>recursive=T))
>
>
>--
>Jose A. Hernandez
>Ph.D. Candidate
>Precision Agriculture Center
>
>Department of Soil, Water, and Climate
>University of Minnesota
>1991 Upper Buford Circle
>St. Paul, MN 55108
>
>Ph. (612) 625-0445, Fax. (612) 625-2208
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


-- 
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA




More information about the R-help mailing list