Malvika
Malvika

Reputation: 71

In R, how to sum all other columns based on value of one column, without specifying column names?

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

Answers (1)

akrun
akrun

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

Related Questions