如何在几列中按一个列分组统计一个因素的出现? [英] How do I count the occurrences of a factor in several columns, grouping by one column?

查看:74
本文介绍了如何在几列中按一个列分组统计一个因素的出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看似简单的问题,但我无法弄清楚如何获得自己想要的东西。

I have a seemingly simple question, but I cannot figure out how to get exactly what I want.

我的数据如下:

      Job     C/C++     Java     Python
  Student     FALSE     TRUE      FALSE
Developer      TRUE     TRUE       TRUE
Developer      TRUE     TRUE      FALSE
 Sysadmin      TRUE    FALSE      FALSE
  Student     FALSE     TRUE       TRUE

我想按作业列,并计算每列中 TRUE s的数量。我想要的输出如下所示:

I would like to group by the "Job" column and count the number of TRUEs in each column. My desired output would look like this:

      Job     C/C++     Java     Python
  Student         0        2          1
Developer         2        2          1 
 Sysadmin         1        0          0

任何帮助将不胜感激。

推荐答案

替代 plyr data.table 解决方案:

data.table:

require(data.table)
tmp.dt <- data.table(temp, key="Job")
tmp.dt[, lapply(.SD, sum), by=Job]

#         Job C.C.. Java Python
# 1: Developer     2    2      1
# 2:   Student     0    2      1
# 3:  Sysadmin     1    0      0

plyr:

require(plyr)
ddply(temp, .(Job), function(x) colSums(x[-1]))

#         Job C.C.. Java Python
# 1 Developer     2    2      1
# 2   Student     0    2      1
# 3  Sysadmin     1    0      0

编辑::如果不是TRUE / FALSE,您必须计算 Newbie 的数量,然后:

If instead of TRUE/FALSE, you've to count the number of Newbie's, then:

使用data.table:

With data.table:

require(data.table)
tmp.dt <- data.table(temp, key="Job")
tmp.dt[, lapply(.SD, function(x) sum(x == "Newbie")), by=Job]

使用plyr:

require(plyr)
ddply(temp, .(Job), function(x) colSums(x[-1] == "Newbie"))

这篇关于如何在几列中按一个列分组统计一个因素的出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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