【发布时间】:2021-03-04 16:45:30
【问题描述】:
我想按组g 交叉表变量x、y、z。我可以用 3 个变量 (tabyl(x,y,z)) 来做到这一点,但我想对 g 的每个值重复它。我试过group_by(g),但没用。
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(janitor)
#>
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#>
#> chisq.test, fisher.test
df <- tibble(x = c(1, 2, 3, 3, 4, 3),
y = c(1,1,1,2,2,2),
z = c(1,2,1,1,2,2),
g = c(1,1,1,1,2,2))
t <- df %>%
tabyl(x,y,z)
t
#> $`1`
#> x 1 2
#> 1 1 0
#> 2 0 0
#> 3 1 1
#> 4 0 0
#>
#> $`2`
#> x 1 2
#> 1 0 0
#> 2 1 0
#> 3 0 1
#> 4 0 1
Created on 2021-03-04 by the reprex package (v1.0.0)
【问题讨论】: