user1584400
user1584400

Reputation: 55

Group by column, categorize and count string occurrences

I have a Problem, which I couldn't solve on my own; my table looks something like that:

Type    Model
-----------------
Car     Mercedes
Car     BMW
Plane   Airbus
Car     BMW
Car     Audi
Plane   Boeing
Plane   Airbus
Car     Mercedes
Car     BMW

What I want to do, is group my Models by Type and also aggregate and count their frequency, it would look something like that:

Car       6
Mercedes  2
BMW       3
Audi      1

Plane     3
Airbus    2
Boeing    1

Would be really nice, if you could help me. Until now I just found a solution for counting the frequencies in general, but not for categorizing my values at the same time as well.

Upvotes: 0

Views: 707

Answers (1)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193507

table should do pretty much what you want.

table(rev(x))
#           Type
# Model      Car Plane
#   Airbus     0     2
#   Audi       1     0
#   BMW        3     0
#   Boeing     0     1
#   Mercedes   2     0

You might also be interested in addmargins:

addmargins(table(rev(x)))
#           Type
# Model      Car Plane Sum
#   Airbus     0     2   2
#   Audi       1     0   1
#   BMW        3     0   3
#   Boeing     0     1   1
#   Mercedes   2     0   2
#   Sum        6     3   9

addmargins(table(rev(x)), 1) would give you just the column sums instead of both columns and rows.


If that's not to your liking, you might try including a split or by or tapply approach:

with(x, by(Model, Type, function(x) data.frame(addmargins(table(droplevels(x))))))
# Type: Car
#       Var1 Freq
# 1     Audi    1
# 2      BMW    3
# 3 Mercedes    2
# 4      Sum    6
# -------------------------------------------------------------------- 
# Type: Plane
#     Var1 Freq
# 1 Airbus    2
# 2 Boeing    1
# 3    Sum    3

Upvotes: 3

Related Questions