gpt4 book ai didi

R dplyr - 在所有列中都不同

转载 作者:行者123 更新时间:2023-12-01 13:45:59 25 4
gpt4 key购买 nike

有没有办法指定 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

对比
df %>% distinct(everything(),.keep_all=FALSE) # with syntax of this form

最佳答案

您可以使用以下代码区分所有列。

library(dplyr)
library(data.table)

df <- data_frame(
id = c(1, 1, 2, 2, 3, 3),
value = c("a", "a", "b", "c", "d", "d")
)
# A tibble: 6 × 2
# id value
# <dbl> <chr>
# 1 1 a
# 2 1 a
# 3 2 b
# 4 2 c
# 5 3 d
# 6 3 d

# distinct with Non-Standard Evaluation
df %>% distinct()

# distinct with Standard Evaluation
df %>% distinct_()

# Also, you can set the column names with .dots.
df %>% distinct_(.dots = names(.))
# A tibble: 4 × 2
# id value
# <dbl> <chr>
# 1 1 a
# 2 2 b
# 3 2 c
# 4 3 d

# distinct with data.table
unique(as.data.table(df))
# id value
# 1: 1 a
# 2: 2 b
# 3: 2 c
# 4: 3 d

关于R dplyr - 在所有列中都不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36191236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com