I would like to calculate a sample mean in R by introducing a specific criteria. For example I have this table and I want the means of only those for whom stage = 1 or 2:
treatment session period stage wage_accepted type
1 1 1 1 25 low
1 1 1 3 19 low
1 1 1 3 15 low
1 1 1 2 32 high
1 1 1 2 13 low
1 1 1 2 14 low
1 1 2 1 17 low
1 1 2 4 16 low
1 1 2 5 21 low
The desired out in this case should be:
stage mean
1 21.0
2 19.6667
Thanks in advance.
Check this out. It's a toy example, but data.table is so compact. dplyr is great as well obviously.
In terms of your need for speed... data.table is a rocket ship look it up. I'll leave it to you to apply this to your question. Best, M2K
You can do this and then later filter for Stages as per your requirement
With the
dplyr
libraryIf you are new to
dplyr
a bit of explanation:Take the data frame
df
thenfilter
wherestage
is equal to 1 or 2. Then for eachgroup
instage
calculate themean
of thewage_accepted
Assuming you have a csv file for the data, you can read data into a data frame using:
Then you can use either this code relying on
sapply()
:Or this code relying on
tapply()
: