如何交叉制表两个变量以对 R 中的第三个分类变量进行分类
How to crosstabulate two variables to classify a third categorical variable in R
我想通过 y
对 x
进行交叉制表,以在 table 单元格中获得 z
的值。
library(tidyverse)
df <- tibble(x = c("a", "a", "b", "b"),
y = c("c", "d", "c", "d"),
z = c("e", "g", "f", "h"))
# I want to obtain this result:
# c d
# a e g
# b f h
Created on 2021-07-18 by the reprex package (v2.0.0)
我想你想要tidyr::pivot_wider
...
df %>% pivot_wider(names_from = y, values_from = z)
# A tibble: 2 x 3
x c d
<chr> <chr> <chr>
1 a e g
2 b f h
我想通过 y
对 x
进行交叉制表,以在 table 单元格中获得 z
的值。
library(tidyverse)
df <- tibble(x = c("a", "a", "b", "b"),
y = c("c", "d", "c", "d"),
z = c("e", "g", "f", "h"))
# I want to obtain this result:
# c d
# a e g
# b f h
Created on 2021-07-18 by the reprex package (v2.0.0)
我想你想要tidyr::pivot_wider
...
df %>% pivot_wider(names_from = y, values_from = z)
# A tibble: 2 x 3
x c d
<chr> <chr> <chr>
1 a e g
2 b f h