省略并找到R中的平均值 [英] Omitting and finding average in R
问题描述
我得到了商店ID和商店赚取的金额.我想做的是,省略一个商店以外的所有商店(在这种情况下,假设商店ID:333333和222222),然后找到平均商店111111的数量.
I am given stores ID's and the amount the store earned. What I would like to do is, omit all but one store (lets say store ID: 333333 and 222222 in this case) and then find the average amount of store 111111.
YEAR STORE ID AMOUNT
2012 111111 11
2012 222222 12
2012 111111 4
2012 222222 4
2012 111111 45
2012 333333 7
感谢所有帮助!
推荐答案
虽然mean(df$AMOUNT[df[, "STORE ID"] == 1111111])
适用于您的特定示例,但您可能还需要检出dplyr软件包,该软件包提供了一些高级的表操作和分组功能.
While mean(df$AMOUNT[df[, "STORE ID"] == 1111111])
will work for your specific example, you might also want to checkout the dplyr package which provides some advanced table manipulation and grouping functions.
例如,要一次获取所有商店的均值,可以执行以下操作:
For example, to get the mean for all stores at once, you could do the following:
library(dplyr)
summarize(group_by(df, STORE.ID), Average = mean(AMOUNT))
或者,使用相同的代码,但使用管道运算符(%>%
),通常在dplyr中完成:
Or, the same code but using the pipe operator (%>%
), which is typically done in dplyr:
df %>%
group_by(STORE.ID) %>%
summarise(Average = mean(AMOUNT))
假设:
- 您的数据在名为df的数据帧中
- 商店ID"列将转换为有效的R名称,并使用点号代替空格
这篇关于省略并找到R中的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!