数据框各列中的唯一值 [英] Unique values in each of the columns of a data frame

查看:71
本文介绍了数据框各列中的唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取数据帧每一列中唯一值的数量。
假设我有以下数据框:

I want to get the number of unique values in each of the columns of a data frame. Let's say I have the following data frame:

DF <- data.frame(v1 = c(1,2,3,2), v2 = c("a","a","b","b"))

然后应该返回v1有3个不同的值,v2有2个。

then it should return that there are 3 distinct values for v1, and 2 for v2.

我尝试了unique(DF),但是

I tried unique(DF), but it does not work as each rows are different.

推荐答案

或使用 unique 无效:

rapply(DF,function(x)length(unique(x)))
v1 v2 
 3  2 

这篇关于数据框各列中的唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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