与两个或多个字段/变量上的SELECT DISTINCT等效 [英] R equivalent of SELECT DISTINCT on two or more fields/variables
问题描述
说我有一个包含两列或更多列的数据框df,有没有一种简单的方法来使用 unique()
或其他R函数来创建以下项的唯一组合的子集两个或更多列?
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?
我知道我可以使用 sqldf()
并编写简单的 SELECT DISTINCT var1,var2,... varN
查询,但是我正在寻找一种R的方法。
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.
它我想到尝试将 ftable 强制为 dataframe 并使用字段名称,但是我也得到了数据集中不存在的组合的交叉列表:
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))
推荐答案
独特
适用于 data.frame
,因此 unique(df [c( var1, var2)])
应该是您想要的。
unique
works on data.frame
so unique(df[c("var1","var2")])
should be what you want.
另一个选项是 dplyr
包:
Another option is distinct
from dplyr
package:
df %>% distinct(var1, var2) # or distinct(df, var1, var2)
注意:
对于较旧版本的dplyr(< 0.5.0,2016-06-24 )与众不同
需要额外的步骤
For older versions of dplyr (< 0.5.0, 2016-06-24) distinct
required additional step
df %>% select(var1, var2) %>% distinct
(或较旧的方式 distinct(select(df,var1,var2))
)。
这篇关于与两个或多个字段/变量上的SELECT DISTINCT等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!