Reputation: 169
For reference
library(vcd)
data(Arthritis)
Art = Arthritis[c("Treatment", "Sex", "Age")]
I want to find out the number of matching attributes in a data frame.
For example
Adj Name Verb
Red John Jumps
Blue John Sleeps
Red John Jumps
Red Smith Jumps
Red Smith Walks
In the end, I want to see:
Adj Name Verb Freq
Red John Jumps 2
Blue John Sleeps 1
Red Smith Jumps 1
Red Smith Walks 1
Is there a way to do this in R?
Upvotes: 0
Views: 24
Reputation: 226077
Slightly clunkier than @G5W's, but:
## cross-tabulate
t1 <- with(dd,table(Adj,Name,Verb))
## convert to long format
res <- as.data.frame(t1)
## drop zeros
subset(res,Freq>0)
Upvotes: 1
Reputation: 37641
You can do this with aggregate
.
DAT = read.table(text="Adj Name Verb
Red John Jumps
Blue John Sleeps
Red John Jumps
Red Smith Jumps
Red Smith Walks",
header=TRUE)
aggregate(rep(1, nrow(DAT)), DAT, length)
Adj Name Verb x
1 Red John Jumps 2
2 Red Smith Jumps 1
3 Blue John Sleeps 1
4 Red Smith Walks 1
You could also use sum
instead of length.
Upvotes: 3