在'dplyr'库中使用'select'功能选择唯一的值 [英] Select unique values with 'select' function in 'dplyr' library

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

问题描述

可以使用 select data.frame 的列中选择所有唯一值c $ c>函数在 dplyr 库中?
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

您还可以同时选择列的一个子集, code> distinct() call,如果您使用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天全站免登陆