How to make a two by two table from a tibble

I have a little tibble that looks like

library(magrittr)
library(tidyverse)
summaryTable <- tibble(admit = c(0,0,1,1),
                       female = c(0,1,0,1),
                       average = c(3.31, 3.36, 3.59, 3.59))
summaryTable %>% print()
#> # A tibble: 4 × 3
#>   admit female average
#>   <dbl>  <dbl>   <dbl>
#> 1     0      0    3.31
#> 2     0      1    3.36
#> 3     1      0    3.59
#> 4     1      1    3.59

Created on 2022-05-13 by the reprex package (v2.0.1)


I would like to make this into a two-by-two table with admit giving the rows, female the columns, and average the cell values. This must be an application of pivot_wider(), but I'm not seeing it.
library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
summaryTable <- tibble(admit = c(0,0,1,1),
                       female = c(0,1,0,1),
                       average = c(3.31, 3.36, 3.59, 3.59))

pivot_wider(summaryTable,names_from = "female",names_prefix = "F",values_from = "average")
#> # A tibble: 2 x 3
#>   admit    F0    F1
#>   <dbl> <dbl> <dbl>
#> 1     0  3.31  3.36
#> 2     1  3.59  3.59

Created on 2022-05-13 by the reprex package (v2.0.1)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.