So I have two vectors of data points, and I would like to make a list of each unique pair, along with that pair's frequency. I know that I can use table to do this with one of the vectors, but I can't seem to figure out how to make it do it with pairs.
it's just...
dat <- data.frame(x = sample(letters[1:3], size = 100, replace = TRUE),
y = sample(letters[1:3], size = 100, replace = TRUE) )
unique(dat)
table(dat)
or, say your vectors are just x, and y and you only want the table...
table(x,y)
# A sample dataset:
dat <- data.frame(x = sample(letters, size = 1000, replace = TRUE),
y = sample(letters, size = 1000, replace = TRUE)
)
# Aggregating using just base R:
as.data.frame(table(dat$x, dat$y))
# With plyr
library(plyr)
count(dat, vars = c(x, y))
count(dat) # Or, less generalizably
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With