【问题标题】:How to crosstabulate two variables to classify a third categorical variable in R如何对两个变量进行交叉制表以对 R 中的第三个分类变量进行分类
【发布时间】:2021-09-26 12:43:07
【问题描述】:

我想将xy 交叉制表以在表格单元格中获得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)

【问题讨论】:

    标签: r crosstab


    【解决方案1】:

    我想你想要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    
    

    【讨论】:

      猜你喜欢
      • 2013-02-10
      • 2020-11-17
      • 1970-01-01
      • 2023-02-03
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多