条件计数和分组在R [英] Conditional count and group by in R

查看:510
本文介绍了条件计数和分组在R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果符合条件 x == 0 ,我想计算类型

I would like to count how many rows there are per type if they meet the condition x == 0. Sort of like a group by in SQL

以下是数据示例

  type    x    
search    0 
NULL      0 
public    0
search    1
home      0
home      1
search    0


推荐答案

我假设你想要找到行数

如果是这种情况,那么我想你有x作为一个变量表示在一个特定的条件柱。 x可以有多个值。假设你想在x为0时找到你的数据中有多少行。这可以通过:

If this is the case, then I suppose you have "x" as a variable represented in a column. "x" can take multiple values. Suppose you want to find how many rows are there in your data when x is 0. This could be done by:

nrow(subset(data, x=="0")

'data'是数据集的对象名称在R中

'data' is the object name for your dataset in R

编辑:

我现在看到您编辑的数据框可以使用这个来解决你的问题:

I am seeing your edited dataframe now. You could use this to solve your problem:

table(data$type, data$x)

这篇关于条件计数和分组在R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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