![](/img/trans.png)
[英]R - Group by on continuous variable headers with categorical variable factors as rows and aggregated as min, max, mean
[英]How do I select the first 84 rows of my x categorical variable (exposed) to compute the mean of my continuous y variable using r?
我尝试使用平均 function 以及摘要。 我也尝试过 tapply 并尝试了 select 行,但它计算了整体平均值。
像这样的东西?
library(tidyverse)
data <- tibble(
x = factor(c("A", "B", "C")) %>% sample(100, replace = TRUE),
y = rnorm(100)
)
data
#> # A tibble: 100 x 2
#> x y
#> <fct> <dbl>
#> 1 B -0.271
#> 2 C -0.361
#> 3 C 1.17
#> 4 A -0.652
#> 5 A 0.770
#> 6 C -0.605
#> 7 B 0.976
#> 8 B 0.392
#> 9 B 1.08
#> 10 A 0.548
#> # ... with 90 more rows
head_means <-
head(data, 84) %>%
group_by(x) %>%
summarize_at("y", mean) %>%
ungroup()
head_means
#> # A tibble: 3 x 2
#> x y
#> <fct> <dbl>
#> 1 A 0.132
#> 2 B 0.385
#> 3 C -0.110
由代表 package (v0.3.0) 于 2019 年 10 月 19 日创建
随意将此或其变体合并到您的问题中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.