计算变量的唯一级别数 [英] Count number of unique levels of a variable

查看:80
本文介绍了计算变量的唯一级别数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一种简单的方法来计算数据框的一列中不同类别的数量.

I am trying to get a simple way to count the number of distinct categories in a column of a dataframe.

例如,在虹膜数据框中,有150行,其中一列是种类,其中有3种不同的种类.我希望能够运行这段代码,并确定该列中有3种不同的种类.我不在乎每个唯一条目对应多少行,只是有多少个不同的变量,这主要是我在研究中发现的.

For example, in the iris data frame, there are 150 rows with one of the columns being species, of which there are 3 different species. I want to be able to run this bit of code and determine that there are 3 different species in that column. I do not care how many rows each of those unique entries correspond to, just how many distinct variables there are, which is mostly what I found in my research.

我在想这样的事情:

df <- iris
choices <- count(unique(iris$Species))

是否存在像这样简单的解决方案?我看了这些帖子,但是它们要么检查整个数据框架,而不是检查该数据框架中的单个列,要么提供比我期望的更复杂的解决方案.

Does a solution as simple as this exist? I have looked at these posts, but they either examine the entire data frame rather than a single column in that data frame or provide a more complicated solution than what I am hoping for.

计数数据帧中实例的数量

分类变量的出现次数在数据框中(R)

>如何计数数据子集中的唯一字符向量数量

推荐答案

以下应完成该工作:

choices <- length(unique(iris$Species))

这篇关于计算变量的唯一级别数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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