Reputation: 71
I have 401 columns. The 401st column represents the PIN CODE. I want to add all the other 400 columns on the basis of pin code. Can I do this without having to specify all the 400 column names?
Upvotes: 1
Views: 35
Reputation: 886938
We can do a group_by
sum
. With dplyr
, pass the 'PINCODE' as the grouping variable and apply sum
with summarise_all
which gets the sum of all other columns remaining in the data
library(dplyr)
df1 %>%
group_by(PINCODE) %>%
summarise_all(sum)
Or with aggregate
from base R
aggregate(.~ PINCODE, df1, sum)
Upvotes: 2