# 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?

• 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

## 1 Answer

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