Search code examples
rdataframesplitdummy-variable

Separate rows to make dummy rows


Consider this dataframe:

dat <- structure(list(col1 = c(1, 2, 0), col2 = c(0, 3, 2), col3 = c(1, 2, 3)), class = "data.frame", row.names = c(NA, -3L))

  col1 col2 col3
1    1    0    1
2    2    3    2
3    0    2    3

How can one dummify rows? i.e. whenever there is a row with more than 1 non-0 value, separate the row into multiple rows with one non-0 value per row.

In this case, this would be:

  col1 col2 col3
1    1    0    0
2    0    0    1
3    2    0    0
4    0    3    0
5    0    0    2
6    0    2    0
7    0    0    3

Solution

  • You can do:

    library(tidyverse)
    
    dat |> 
      pivot_longer(everything()) |> 
      mutate(id = 1:n()) |> 
      pivot_wider(values_fill = 0) |> 
      filter(!if_all(-id, ~ . == 0)) |> 
      select(-id)
    
    # A tibble: 7 x 3
       col1  col2  col3
      <dbl> <dbl> <dbl>
    1     1     0     0
    2     0     0     1
    3     2     0     0
    4     0     3     0
    5     0     0     2
    6     0     2     0
    7     0     0     3