在dplyr
库中,使用select
函数从data.frame
列中选择所有唯一值是否可行?
类似于SQL
表示法中的"SELECT DISTINCT field1 FROM table1
"。
谢谢!
distinct()
方法来实现这一点。distinct_df = df %>% distinct(field1)
您可以通过以下方式获得不同值的向量:
distinct_vector = distinct_df$field1
同样,在执行distinct()
调用时,您还可以同时选择列的子集,如果使用head/tail/glimpse查看数据帧,则会使代码更加清晰易读。
distinct_df = df %>% distinct(field1) %>% select(field1)
distinct_vector = distinct_df$field1
补充一下其他答案,如果您希望返回一个向量而不是数据框,则有以下选项:
dplyr >= 0.7.0
使用pull
动词:
mtcars %>% distinct(cyl) %>% pull()
dplyr < 0.7.0
使用括号将dplyr函数括起来,再与$
语法结合使用:
(mtcars %>% distinct(cyl))$cyl
dplyr
的 select
函数可以从数据框中选择特定的列。要返回特定列中唯一的值,可以使用 group_by
函数。例如:
library(dplyr)
# Fake data
set.seed(5)
dat = data.frame(x=sample(1:10,100, replace=TRUE))
# Return the distinct values of x
dat %>%
group_by(x) %>%
summarise()
x
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
如果您想更改列名,可以添加以下内容:
dat %>%
group_by(x) %>%
summarise() %>%
select(unique.x=x)
这将从数据框中选择列 x
(从 dplyr
返回的数据框中,当然在这种情况下只有一列),并将其名称更改为 unique.x
。set.seed(5)
dat = data.frame(x=sample(1:10,100, replace=TRUE),
y=sample(letters[1:5], 100, replace=TRUE))
dat %>%
group_by(x,y) %>%
summarise() %>%
select(unique.x=x, unique.y=y)
distinct()
函数。 - hadley
src_postgres()
)直接查询数据库,这个方法就不起作用了。会报错:Error: Can't calculate distinct only on specified columns with SQL
。请注意,这里不能仅基于指定的列进行SQL去重计算。 - djhockingdistinct()
函数的工作方式已经改变。现在,默认情况下,distinct()
只返回用作参数传递给distinct()
的列。如果您想保留其他列,现在必须将.keep_all = TRUE
作为额外的参数传递给distinct()
。 - RoyalTS