Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Transpose, spread or pivot a table with 0 or 1 depending on the value of a column

i have a data table like this one:

a <- data.table::data.table(A = seq(1:5), B = c("V1","V2","V1","V2","V3"))

and i need a result with new columns V1, V2, V3 (there are some more possible values then 3) with 0 or 1 (true or false) depending on the value in B. So the result should look like this one:

   A  B V1 V2 V3
1: 1 V1  1  0  0
2: 2 V2  0  1  0
3: 3 V1  1  0  0
4: 4 V2  0  1  0
5: 5 V3  0  0  1

I have tried pivot_wider with no success.
I have tried spread with diag function with no success.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Does anyone has a solution. Thank you very much.

>Solution :

Using tidyverse:

a %>% mutate(n = 1, B1 = B) %>% 
                 pivot_wider(names_from = B1, values_from = n, values_fill = 0)
# A tibble: 5 × 5
      A B        V1    V2    V3
  <int> <chr> <dbl> <dbl> <dbl>
1     1 V1        1     0     0
2     2 V2        0     1     0
3     3 V1        1     0     0
4     4 V2        0     1     0
5     5 V3        0     0     1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading