-1

Suppose I have data for a number of transaction that occur within different states

    State Cost
    AK, 70
    AK, 75
    AK, 10
    IL, 20
    IL, 1050
    IL, 235
    etc...

How can I compress my data so that I'm only looking at total cost per state? I can only come up with solutions by writing python scripts to compress this data but it seems like R should be able to support this operation.

   State Cost
   AK, 155
   IL, 1305
   etc... 

Any ideas are greatly appreciated.

headbone
  • 51
  • 6

1 Answers1

1
library("dplyr")

options(digits=4)

StatsByState <- group_by(Your.df, State)

summarise(StatsByState, Sum = sum(Cost), Mean = mean(Cost), StDev = sd(Cost))

options(digits=7)

  State  Sum   Mean  StDev

1    AK  155  51.67  36.17

2    IL 1040 346.67 565.80

3    NE  720 240.00 242.49
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245