我想制作一张看起来像这样的桌子
我对每个数据点都有一些小标题,但它们没有合并。
library('dplyr')
library('ISLR')
data(Hitters)
Hitters <- na.omit(Hitters)
Q <- Hitters %>% group_by(League) %>%
dplyr::summarize(count = n(), avg_wage = sum(Salary)/n())
A <- Hitters %>% group_by(Division) %>%
dplyr::summarize(count = n(), avg_wage = sum(Salary)/n())
Z <- Hitters %>% group_by(NewLeague) %>%
dplyr::summarize(count = n(), avg_wage = sum(Salary)/n())
我的目标是通过共享的“ count”和“ avg_wage”列在输出中相互堆叠小标题。我尝试了bind_rows()和ftable(),但没有成功。
答案 0 :(得分:1)
问题是您不能将行与不同的列名组合在一起,因此最终会给您带来混乱的数据框。相反,我们可以使用gather()
创建两个新列并获取正确的表。
library(tidyverse)
library(ISLR)
data(Hitters)
Hitters <- na.omit(Hitters)
Q <- Hitters %>% group_by(League) %>%
dplyr::summarize(count = n(), avg_wage = sum(Salary)/n())
A <- Hitters %>% group_by(Division) %>%
dplyr::summarize(count = n(), avg_wage = sum(Salary)/n())
Z <- Hitters %>% group_by(NewLeague) %>%
dplyr::summarize(count = n(), avg_wage = sum(Salary)/n())
list(Q,A,Z) %>%
map_df(bind_rows) %>%
gather("league_type", "league_id", c(1, 4, 5)) %>%
filter(!is.na(league_id))
#> Warning: attributes are not identical across measure variables;
#> they will be dropped
#> # A tibble: 6 x 4
#> count avg_wage league_type league_id
#> <int> <dbl> <chr> <chr>
#> 1 139 542. League A
#> 2 124 529. League N
#> 3 129 624. Division E
#> 4 134 451. Division W
#> 5 141 537. NewLeague A
#> 6 122 535. NewLeague N
由reprex package(v0.2.1)于2019-01-21创建
您可以使用spread()
将其恢复为宽格式,尽管我建议不要这样做。长版本可能会更容易使用。