3

I'm new to R. Here is my specific question. Let's say I'm working with the following data set called "data" for this example. My headers are state, type, and value.

structure(list(state = structure(c(1L, 1L, 1L, 1L, 2L, 2L), .Label = c("AK", 
"AL"), class = "factor"), type = structure(c(2L, 2L, 1L, 1L, 
2L, 1L), .Label = c(" D", " R"), class = "factor"), value = c(100L, 
200L, 100L, 150L, 100L, 150L)), .Names = c("state", "type", "value"
), class = "data.frame", row.names = c(NA, -6L))



  state type value
1    AK    R   100
2    AK    R   200
3    AK    D   100
4    AK    D   150
5    AL    R   100
6    AL    D   150

I want to write a function that will add up the values for each type and state. For example. For AK type R the output would be 300. For AK type D the output would be 250. For AL type R the output would be 100, and for AL type D the output would be 150.

1
  • 1
    Quick tip for you grasshopper: square brackets around R in the search box returns you all questions in the R tag i.e. search for "[R]". Then click the "voted" tab and scroll through the top questions. Also, over 600 questions contain the word "aggregate" i.e. "[R] aggregate".
    – Matt Dowle
    Dec 30, 2012 at 15:28

5 Answers 5

7

Not plyr, but just aggregate

> aggregate(value~state+type, data=data,FUN=sum)
  state type value
1    AK    D   250
2    AL    D   150
3    AK    R   300
4    AL    R   100
1
  • Thanks for the help! Saved me loads of time. Dec 30, 2012 at 0:34
5

Although @Matthew Lundberg's answer is the best one here's some alternatives.

If you really want to use plyr you could do:

ddply(DF, .(state, type), numcolwise(sum))
  state type value
1    AK    D   250
2    AK    R   300
3    AL    D   150
4    AL    R   100

Here's another solution using reshape2 package

library(reshape2)
dcast( melt(DF), state + type ~ variable, sum)
Using state, type as id variables
  state type value
1    AK    D   250
2    AK    R   300
3    AL    D   150
4    AL    R   100

If you want just a vector then this could be useful:

sapply(with(DF, split(value, list(state, type))), sum)
AK.D  AL.D  AK.R  AL.R 
250   150   300   100 
5

You can just use tapply

data <- read.csv(header=TRUE,text="state, type, value
AK, R, 100
AK, R, 200
AK, D, 100
AK, D, 150
AL, R, 100
AL, D, 150")

tapply(data$value, list(data$state,data$type), sum)
#     D   R
# AK  250 300
# AL  150 100
0
3

A plyr solution would be:

ddply(data, .(state,type),summarise, total=sum(value, na.rm = TRUE))
#   state type total
# 1    AK    D   250
# 2    AK    R   300
# 3    AL    D   150
# 4    AL    R   100
3

For the sake of completeness, there's also the "data.table" package, and by in base R. Assuming your dataset is called "myd":

by(myd$value, list(myd$state, myd$type), FUN=sum)
# : AK
# :  D
# [1] 250
# ------------------------------------------------------------------------------ 
# : AL
# :  D
# [1] 150
# ------------------------------------------------------------------------------ 
# : AK
# :  R
# [1] 300
# ------------------------------------------------------------------------------ 
# : AL
# :  R
# [1] 100

library(data.table)
DT <- data.table(myd)
DT[, sum(value), by = "state,type"]
#    state type  V1
# 1:    AK    R 300
# 2:    AK    D 250
# 3:    AL    R 100
# 4:    AL    D 150

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.