Skip to content
Prev 200449 / 398503 Next

Sum over indexed value

Gunadi wrote:
Supposing you had the data:

  tstData <- data.frame( index = c(1,2,1,1,3,2), 
    value = c( 0, 4, 0, 0, 7, 4 ) )

You could use the by() function to divide the data.frame and sum the value
column:

  sums <- by( tstData, tstData[['index']], function( slice ){

    return( sum( slice[['value']] ) )

  })

However, by() tends to do a poor job of cleanly expressing which values of
'index' generated the sums.  I would recomend the __ply() functions in
Hadley Wickham's plyr package.  Specifically ddply():

  require( plyr )

  sums <- ddply( tstData, 'index', function( slice ){

    return(
      data.frame( sum = sum( slice[['value']] ) )
    )
  })

  sums
   index sum
  1     1   0
  2     2   8
  3     3   7


Hope this helps!

-Charlie

-----
Charlie Sharpsteen
Undergraduate
Environmental Resources Engineering
Humboldt State University