rreshape2

dcasting a dataframe using reshape2::dcast


I am trying to reshape a dataframe using dcast function of reshape2 package

library(reshape2)
dat = data.frame(aa = c('A', 'B', 'C', 'C', 'B'))
dcast(dat, aa~aa)

This generates below output

  aa A B C
1  A 1 0 0
2  B 0 2 0
3  C 0 0 2

However I wanted to keep the number of rows same as my original dataframe as below

enter image description here

Is there any direct function to get my desired shape?

Thanks for your time.


Solution

  • With tidyverse or base R you can do:

    dat = data.frame(aa = c('A', 'B', 'C', 'C', 'B'))
                      
    library(tidyverse)
    dat %>% 
      mutate(id = seq_along(aa), val = 1) %>% 
      tidyr::pivot_wider(names_from = aa, values_from = val, values_fill = 0) %>% 
      select(-id)
    #> # A tibble: 5 × 3
    #>       A     B     C
    #>   <dbl> <dbl> <dbl>
    #> 1     1     0     0
    #> 2     0     1     0
    #> 3     0     0     1
    #> 4     0     0     1
    #> 5     0     1     0
    
    as.data.frame(sapply(unique(dat$aa), \(x) as.numeric(x == dat$aa)))
    #>   A B C
    #> 1 1 0 0
    #> 2 0 1 0
    #> 3 0 0 1
    #> 4 0 0 1
    #> 5 0 1 0