用dplyr计算R中的平均值并计数 [英] Average and count with aggregation in R with dplyr

查看:383
本文介绍了用dplyr计算R中的平均值并计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算可变价格等于0、49的元素数,或者如果我也想要计数,但是在这种情况下我不在乎价格是多少。
如果此终端不等于0或49,我想按终端平均价格

I want to count the number of elements for the variable price equal to 0, 49 or if something else I also want the count but i don't care how many the price is in this case. I want to do an average of price by terminal if this one is not equal to 0 or 49

terminal <- c("a", "b", "a", "c", "b", "b")
price <- c(0, 49, 3.5, 0, 17, 32)
df <- data.frame(terminal, price)

df %>%
group_by(terminal, price) %>%
summarise(count = n())

在这里,我要具有以下计数:1,1,2,1, 1
,此后,我想获得该终端的价格不等于零或49时的平均价格。

Here I want to have this count : 1, 1, 2, 1, 1 and after this I'd like to get an average of the price of terminal when this one is not equal to zero or 49.

推荐答案

我们需要基于值 0和 49创建一个具有价格的分组变量。为此,一种方法是使用 == 来获得一个同时具有0和49的逻辑索引,并对它们进行一些算术运算,以便我们将3个组对应为0 ,一个为49,其余为所有其他。通过终端和 gr分组,我们总结以获取行数,即 n和平均值的价格不为0或49(使用%in%和否定的

We need to create a grouping variable with 'price' based on the values '0' and '49'. For this, one way would be to use == to get a logical index with both 0 and 49, do some arithmetic to so that we will be having 3 groups one for 0, one for 49, and the rest for all others. Grouped by 'terminal' and 'gr', we summarise to get the number of rows i.e. 'n' and the mean of 'price' that are not 0, or 49 (using %in% and the negation !)

library(dplyr)
df %>% 
    group_by(terminal, gr= 1+2*(price==0)+4*(price==49)) %>% 
    summarise(n=n(), 
              Avg = mean(price[!(price %in% c(0,49))], na.rm=TRUE))

这篇关于用dplyr计算R中的平均值并计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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