如何计算给定因子中每个级别的值? [英] How to count how many values per level in a given factor?

查看:22
本文介绍了如何计算给定因子中每个级别的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大约 2500 行的 data.frame mydf.这些行对应于第 1 列 mydf$V1 中的 69 类对象,我想计算每个对象类有多少行.我可以通过以下方式获得这些类的一个因素:

I have a data.frame mydf with about 2500 rows. These rows correspond to 69 classes of objects in colum 1 mydf$V1, and I want to count how many rows per object class I have. I can get a factor of these classes with:

objectclasses = unique(factor(mydf$V1, exclude="1"));

计算每个对象类的行数的简洁 R 方法是什么?如果这是任何其他语言,我会用循环遍历数组并保持计数,但我是 R 编程的新手,并且正在尝试利用 R 的矢量化操作.

What's the terse R way to count the rows per object class? If this were any other language I'd be traversing an array with a loop and keeping count but I'm new to R programming and am trying to take advantage of R's vectorised operations.

推荐答案

或者使用 dplyr 库:

library(dplyr)
set.seed(1)
dat <- data.frame(ID = sample(letters,100,rep=TRUE))
dat %>% 
  group_by(ID) %>%
  summarise(no_rows = length(ID))

注意%>%的使用,与bash中管道的使用类似.有效地,上面的代码将dat 输送到group_by,并将该操作的结果输送到summarise.

Note the use of %>%, which is similar to the use of pipes in bash. Effectively, the code above pipes dat into group_by, and the result of that operation is piped into summarise.

结果是:

Source: local data frame [26 x 2]

   ID no_rows
1   a       2
2   b       3
3   c       3
4   d       3
5   e       2
6   f       4
7   g       6
8   h       1
9   i       6
10  j       5
11  k       6
12  l       4
13  m       7
14  n       2
15  o       2
16  p       2
17  q       5
18  r       4
19  s       5
20  t       3
21  u       8
22  v       4
23  w       5
24  x       4
25  y       3
26  z       1

请参阅 dplyr 介绍,了解更多上下文,以及有关各个功能的详细信息的文档.

See the dplyr introduction for some more context, and the documentation for details regarding the individual functions.

这篇关于如何计算给定因子中每个级别的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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