简体   繁体   English

根据另一列中的多项选择拉取一列的平均值

[英]Pulling the average of one column based on multiple selections in another column

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.我正在尝试根据 x 列中的一系列值来拉取 y 的平均值。 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.例如,如果 x 大约为 0.86、0.90 和 0.91,我想构建的代码给出平均 y。 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.我得到的答案只是 group_by 语句真假的平均值。 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?如何重新排列代码以便只获得我正在寻找的三个平均值 (0.86,0.90,0.91) 而不是真正的错误平均值?

You can filter the averages you are looking for after performing summarise() with the %in% operator.您可以在使用%in%运算符执行summarise()后过滤您正在寻找的平均值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM