mpg
mpg

Reputation: 3919

Group by crosstab in R

I am trying to do something rather simple in R (I think) but cannot figure out how. One of the nice things that SPSS can do for me is quick table of a summary statistic (ie average) by a categorical variable.

So I have a category within my dataframe calle TC Code and a numeric variable called AMT. All I want is the average AMT by TC code

TC Code    AMT Ave
A          $ Ave value
B          $ Average Value
etc

I keep trying to do something with the CrossTable function by no luck.

Upvotes: 0

Views: 2753

Answers (1)

Arun
Arun

Reputation: 118889

Please provide reproducible code (generate some data and explain what you require).

Some data:

set.seed(45)
df <- data.frame(code = sample(LETTERS[1:8], 
           50, replace=TRUE), val = sample(50))

Using aggregate:

aggregate(data = df, val ~ code, mean)
  code      val
1    A 19.33333
2    B 26.00000
3    C 27.72727
4    D 21.50000
5    E 29.66667
6    F 30.20000
7    G 13.00000
8    H 24.50000

Upvotes: 2

Related Questions