问题
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:
- Your data is in a data frame called df
- 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