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

Expanding dataframe in R

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:

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

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
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