I have the following data in R:
Group | Dummy_N | Total_N | Other val |
---|---|---|---|
1 | 1 | 4 | x |
2 | 2 | 3 | y |
I want to expand this data so that there are Total_N number of rows with Dummy_N number of 1s.Like:
Group | Dummy_N | Total_N | Other val |
---|---|---|---|
1 | 1 | 4 | x |
1 | 0 | 4 | x |
1 | 0 | 4 | x |
1 | 0 | 4 | x |
2 | 1 | 3 | y |
2 | 1 | 3 | y |
2 | 0 | 3 | y |
I am able to expand it with the following code:
df.expanded <- df[rep(row.names(df), df$Total_N), 1:4]
But I am struggling to implement making the Dummy N which corresponds to the number in Dummy_N in the first table.
>Solution :
An option in tidyverse
would be to expand the data with uncount
(similar to rep(...)
in base R
), then convert the ‘Dummy_N’ column to binary by creating a logical vector with the sequence (row_number()
) and its value, grouped by ‘Group’ column
library(dplyr)# version >= 1.1.0
library(tidyr)
df %>%
uncount(Total_N, .remove = FALSE) %>%
mutate(Dummy_N = +(row_number() <= Dummy_N), .by = Group)
-output
Group Dummy_N Total_N Otherval
1 1 1 4 x
2 1 0 4 x
3 1 0 4 x
4 1 0 4 x
5 2 1 3 y
6 2 1 3 y
7 2 0 3 y