R equivalent of SELECT DISTINCT on two or more fields/variables
Posted
by wahalulu
on Stack Overflow
See other posts from Stack Overflow
or by wahalulu
Published on 2010-05-24T21:38:19Z
Indexed on
2010/05/24
21:41 UTC
Read the original article
Hit count: 154
Say I have a dataframe df with two or more columns, is there an easy way to use unique() or other R function to create a subset of unique combinations of two or more columns?
I know I can use sqldf() and write an easy "SELECT DISTINCT var1, var2, ... varN" query, but I am looking for an R way of doing this.
It occurred to me to try ftable coerced to a dataframe and use the field names, but I also get the cross tabulations of combinations that don't exist in the dataset:
uniques <- as.data.frame(ftable(df$var1, df$var2))
© Stack Overflow or respective owner