Reputation: 366
Let's say that I have the following data frame with three columns.
data = data.frame(id=c(1:10), interest_1=c("food","","","drugs","beer","soda","","","drugs","sports"),
interest_2=c("fruits","car","jeans","","","","soda","shoes","","drugs"),
interest_3=c("","","","","soda","sports","","","",""))
data
I want to get a count of each row.
The following incident, where food is interest_1, fruits is interest_2, and nothing is interest_3 occurs only once.
id interest_1 interest_2 interest_3
1 1 food fruits
The following incident, where drugs are interest_1 and nothing is interest_2 or interest_3 occurs twice.
id interest_1 interest_2 interest_3
4 drugs
9 drugs
I want to get a count the number of times that each incidence occurs. How would I go about doing this?
Output should look like:
interest_1 interest_2 interest_3 count
food fruits 1
car 1
jeans 1
drugs 2
Upvotes: 0
Views: 2802
Reputation: 4030
require(plyr)
ddply(data, .(interest_1, interest_2, interest_3), c("nrow"))
Upvotes: 3
Reputation: 8488
> aggregate(id~.,data,length)
interest_1 interest_2 interest_3 id
1 drugs 2
2 car 1
3 sports drugs 1
4 food fruits 1
5 jeans 1
6 shoes 1
7 soda 1
8 beer soda 1
9 soda sports 1
Basically, this means: apply function length
to to the vector made up of id
values for each combination of the other columns.
Upvotes: 6