Jay
Jay

Reputation: 57

summing up total from specific columns in R

I have a dataset with the following columns

bus Id from to bus fare costs profit from trip
145 ATL ORL 1000 700 300
145 ATL Miami 2000 1200 800
145 ATL ORL 1000 700 300
145 ATL Miami 2000 1200 800
145 ORL Richmond 800 300 500
152 Ricmond Baltimore 400 150 250
152 Richmond NYC 450 250 200
152 Ricmond Baltimore 400 150 250
152 Richmond NYC 450 250 200
165 NYC NJ 100 40 60
165 NYC Philadelphia 200 80 120
165 NYC NJ 100 40 60
165 NYC Philadelphia 200 80 120

I want to create a summary which counts the total of profits and total of costs from all rows if the bus ID is the same. This will help me calculate the most profitable route for each bus. How should I code it?

Upvotes: 2

Views: 40

Answers (1)

akrun
akrun

Reputation: 886928

Using base R

aggregate(.~ `bus Id`, subset(df1, select = `base fare`:`profit from trip`), FUN = sum)

Upvotes: 0

Related Questions