Search code examples
rtidyverse

Mean of variables in a 2-way table in r


I am trying to get a 2-way table with the mean of observations. My dataframe is like this

df
 ID       value  Cl   Dom          Con
 <chr>    <dbl> <chr> <chr>        <chr>     
 G1-MAT1      1 B     Knowing      Ar
 G1-MAT2      0 B     Knowing      Ar
 G1-MAT3      0 B     Knowing      Ar
 G1-MAT4      1 C     Knowing      Ar
 G1-MAT5      1 B     Knowing      Ar
 G1-MAT6      0 B     Reasoning    Me
 G1-MAT7      1 B     Reasoning    Ar
 G1-MAT8      1 C     Reasoning    Ar
 G1-MAT9      1 B     Knowing      Me
 G1-MAT10     0 B     Knowing      Ar

I am trying to get something like this with tidyverse.

  Dom              Ar       Me
  <chr>            <dbl>    <dbl>
  Reasoning        1        0
  Knowing          0.5      1

Where the entries are means of the observations. Is it possible?


Solution

  • With the {tidyverse} you can use group_by() and summarize() and then pivot_wider() to put it in the format you show.

    library(tidyverse)
    
    d <- tibble(value = sample(0:1, 100, T),
                Dom = sample(c("Knowing", "Reasoning"), 100, T),
                Con = sample(c("Ar", "Me"), 100, T))
    
    d %>% 
      group_by(Dom, Con) %>% 
      summarize(value = mean(value, na.rm = T)) %>% 
      pivot_wider(names_from = Con)
    #> # A tibble: 2 × 3
    #> # Groups:   Dom [2]
    #>   Dom          Ar    Me
    #>   <chr>     <dbl> <dbl>
    #> 1 Knowing   0.435 0.391
    #> 2 Reasoning 0.571 0.769
    

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