# aggregate all data by Date and ID [closed]

I have a dataset with 20 different columns and i want to aggregate all of the columns by the first two columns (Date and ID). Is there a way to say aggregate all instead of typing the names of the individual columns?

## closed as off-topic by gung♦, John, Juho Kokkala, Scortchi♦Sep 5 '15 at 12:50

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – gung, John, Juho Kokkala, Scortchi
If this question can be reworded to fit the rules in the help center, please edit the question.

• I think this has been answered here. There seems to be a few different solutions. I think using the aggregate function seems the easiest, but the user chose to use functions from reshape2 as the best answer. – RayVelcoro Aug 27 '15 at 21:03
• @RayVelcoro is there a way though to not type in the names and instead write something that means "select all columns"? – Nick Aug 28 '15 at 14:48

Yes. Use the "." instead of typing everything out. The dot means put everything For example

df1 = read.table("clipboard") #this is my data
year month         x1           x2
2000     1  -80.83405 -224.9540159
2000     2 -223.76331 -288.2418017
2000     3 -188.83930 -481.5601913
2001     5 -197.47797 -473.7137420
2001     5 -259.07928 -372.4563522

aggregate(. ~year, data=df1, sum, na.rm=TRUE)


This will aggregate with the sum everything on year, giving us

aggregate(. ~year, data=df1, sum, na.rm=TRUE)
year month        x1        x2
1 2000     6 -493.4367 -994.7560
2 2001    10 -456.5572 -846.1701


If you also wanted to aggregate on unique year month pairs, then just add a + to the RHS .

aggregate(. ~year+month, data=df1, sum, na.rm=TRUE)
year month         x1        x2
1 2000     1  -80.83405 -224.9540
2 2000     2 -223.76331 -288.2418
3 2000     3 -188.83930 -481.5602
4 2001     5 -456.55725 -846.1701