0

I'm new at r and this website. I have copied a sample of my data below. Basically, I have data that has 29 variables/columns and I have 30 unique row values. I want to aggregate over all the columns that are not community (all but one column). Is there a simpler way to do this? I was going to use:

newdf <- aggregate (Column 1 + Column 2 + Column 3 .... Column 29 ~ Community, df, sum) 

However this is rather tedious and prone to mistakes. Is there another way to do this? Sorry if this has been asked before I couldn't find another post.

Community, Total.Pop., Female.Pop., Male.Pop., White.Alone.
Aleutians, 2697, 1750, 947, 64                                    
Aleutians, 1182, 6831, 499, 2952                                      
Aleutians, 4283, 2830, 1453, 1893                              
Anchorage, 4835, 2477, 2358, 4186                              
Anchorage, 4472, 2278, 2194, 3968                                                                        

This is what I would ideally like:

Community, Total.Pop., Female.Pop., Male.Pop., White.Alone.
Aleutians, 8162, 11411, 2899, 4909
Anchorage, 9307, 4755, 4552, 8154
  • Check out [this](https://stackoverflow.com/a/1661144/7508461) original or [this](https://stackoverflow.com/a/45068290/7508461) on the `mtcars` example from a few minutes ago. In your case this would be `aggregate(.~ Community, df, sum)` – Numb3rs Jul 12 '17 at 22:11
  • This would do it... `aggregate(df[,-1],by=list(df$Community.),sum)` – Andrew Gustar Jul 12 '17 at 22:16
  • @Numb3rs thank you it worked out well! does this question need to be deleted or does it stay up? – Emma Jul 12 '17 at 22:23

0 Answers0