Omitting and finding average in R

允我心安 提交于 2019-12-13 04:48:01

问题


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

All help is appreciated!


回答1:


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))

Or, the same code but using the pipe operator (%>%), which is typically done in dplyr:

df %>%
  group_by(STORE.ID) %>%
  summarise(Average = mean(AMOUNT))

Assumptions:

  1. Your data is in a data frame called df
  2. The STORE ID column is converted to a valid R name with a dot in place of the space


来源:https://stackoverflow.com/questions/27759867/omitting-and-finding-average-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!