有没有办法指定 dplyr::distinct 应该使用所有列名而不诉诸非标准评估?
df <- data.frame(a=c(1,1,2),b=c(1,1,3))
df %>% distinct(a,b,.keep_all=FALSE) # behavior I'd like to replicate
Run Code Online (Sandbox Code Playgroud)
对比
df %>% distinct(everything(),.keep_all=FALSE) # with syntax of this form
Run Code Online (Sandbox Code Playgroud)
您可以使用下面的代码区分所有列。
\n\nlibrary(dplyr)\nlibrary(data.table)\n\ndf <- data_frame(\n id = c(1, 1, 2, 2, 3, 3),\n value = c("a", "a", "b", "c", "d", "d")\n)\n# A tibble: 6 \xc3\x97 2\n# id value\n# <dbl> <chr>\n# 1 1 a\n# 2 1 a\n# 3 2 b\n# 4 2 c\n# 5 3 d\n# 6 3 d\n\n# distinct with Non-Standard Evaluation\ndf %>% distinct()\n\n# distinct with Standard Evaluation\ndf %>% distinct_()\n\n# Also, you can set the column names with .dots.\ndf %>% distinct_(.dots = names(.))\n# A tibble: 4 \xc3\x97 2\n# id value\n# <dbl> <chr>\n# 1 1 a\n# 2 2 b\n# 3 2 c\n# 4 3 d\n\n# distinct with data.table\nunique(as.data.table(df))\n# id value\n# 1: 1 a\n# 2: 2 b\n# 3: 2 c\n# 4: 3 d\nRun Code Online (Sandbox Code Playgroud)\n