Your suggested solution was very close. Here is a slightly modified version that does not return a warning:
data <- read.table(text='
year state somevalue
2000 NY 1000
2000 NY 1200
2000 NY 1100
2001 NY 2000
2001 NY 2200
', header=TRUE)
aggregate(data$somevalue, list(data$state, data$year), sum)
Although, @lukeA's solutions and @Livius's solution are better in base R
because they return the desired column names in the requested order.
This returns two of the three requested column names:
aggregate(data$somevalue, list(state=data$state, year=data$year), sum)
My second solution differs from LukeA's second solution only in that I did not use the with
function and did not label the aggregate
options. His answer is better.