Search code examples
rsummary

Using R (and dplyr) to create multiple summary tables with factor information


I'm an R beginner and am trying to take tables that look like this

ID Type
X  A
X  A
X  B
Y  C
Y  D
Y  E
Z  F
Z  F

And create summary tables for each ID value so they'll look like this

X  Y  Z  
A  C  F
B  D
   E 

How would I go about doing this with dplyr or something similar? I've tried doing something like a crosstab, but since there are no numberic values I haven't been able to.


Solution

  • Here is one approach using tidyverse:

    library(tidyverse)
    
    df %>%
      group_by(ID) %>%
      distinct(ID, Type) %>%
      mutate(rn = row_number()) %>%
      pivot_wider(id_cols = rn, names_from = ID, values_from = Type) %>%
      replace(., is.na(.), " ") %>%
      as.data.frame() %>%
      select(-rn)
    

    Output

      X Y Z
    1 A C F
    2 B D  
    3   E  
    

    Data

    df <- data.frame(
      ID = c('X', 'X', 'X', 'Y', 'Y', 'Y', 'Z', 'Z'),
      Type = c('A', 'A', 'B', 'C', 'D', 'E', 'F', 'F'),
      stringsAsFactors = FALSE
    )