I have a data set that looks like this:
x y
1 0.850 85.0
2 0.855 85.5
3 0.860 86.0
4 0.865 86.5
5 0.870 87.0
6 0.875 87.5
7 0.880 88.0
8 0.885 88.5
9 0.890 89.0
10 0.895 89.5
11 0.900 90.0
12 0.905 90.5
13 0.910 91.0
14 0.915 91.5
15 0.920 92.0
16 0.925 92.5
17 0.930 93.0
18 0.935 93.5
19 0.940 94.0
20 0.945 94.5
21 0.950 95.0
structure(list(x = c(0.85, 0.855, 0.86, 0.865, 0.87, 0.875, 0.88,
0.885, 0.89, 0.895, 0.9, 0.905, 0.91, 0.915, 0.92, 0.925, 0.93,
0.935, 0.94, 0.945, 0.95), y = c(85, 85.5, 86, 86.5, 87, 87.5,
88, 88.5, 89, 89.5, 90, 90.5, 91, 91.5, 92, 92.5, 93, 93.5, 94,
94.5, 95)), class = "data.frame", row.names = c(NA, -21L))
I am trying to pull averages of y based on a range of values in the x column. For instance, I would like to build code that gives me the average y if x is approximately 0.86, 0.90, and 0.91. The code I tried to build looks like this:
library(dplyr)
data%>%group_by(round(x,digits = 2)==c(0.86,0.90,0.91))%>%select(y)%>%
summarise_each(funs(mean(., na.rm = TRUE)))
The answer I am getting is just the averages for the true or false of the group_by statement. How do I rearrange the code so that I get just the three averages I am looking for (0.86,0.90,0.91) instead of a true false average?
CodePudding user response:
You can filter the averages you are looking for after performing summarise()
with the %in%
operator.
library(tidyverse)
data %>%
mutate(x = round(x, 2)) %>%
group_by(x) %>%
summarise(across(everything(), mean)) %>%
filter(x %in% c(0.86, 0.90, 0.91))