使用“dplyr"库中的“select"函数选择唯一值 [英] Select unique values with 'select' function in 'dplyr' library

查看:25
本文介绍了使用“dplyr"库中的“select"函数选择唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 dplyr 中的 select 函数从 data.frame 的列中选择所有 唯一 值?代码>库?类似于 SQL 符号中的SELECT DISTINCT field1 FROM table1".

Is it possible to select all unique values from a column of a data.frame using select function in dplyr library? Something like "SELECT DISTINCT field1 FROM table1" in SQL notation.

谢谢!

推荐答案

在 dplyr 0.3 中,这可以使用 distinct() 方法轻松实现.

In dplyr 0.3 this can be easily achieved using the distinct() method.

这是一个例子:

distinct_df = df %>% distinct(field1)

您可以通过以下方式获得不同值的向量:

You can get a vector of the distinct values with:

distinct_vector = distinct_df$field1

您还可以在执行 distinct() 调用的同时选择列的子集,如果您使用 head/tail/glimpse 检查数据框,则可以更清晰地查看.:

You can also select a subset of columns at the same time as you perform the distinct() call, which can be cleaner to look at if you examine the data frame using head/tail/glimpse.:

distinct_df = df %>% distinct(field1) %>% select(field1)distinct_vector = distinct_df$field1

这篇关于使用“dplyr"库中的“select"函数选择唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆