1

This is a really easy question but for some reason I cannot find the answer on R cheat sheets.

I have a table that looks like this:

Number of People     Income Per Person
5                    800
4                    300
6                    200

I want to expand the number of rows so that I have one row for each person, instead of one row that just says, "Number of people." So, 5 + 4 + 6 = total of 15 rows. How can I do this using dplyr or tidyr?

Thank you! - New R User

Anna Jones
  • 91
  • 7
  • 1
    ```setDT(df1)[rep(seq_len(nrow(df1)), df1$Number_of_People),][, .(ID = 1:max(Number_of_People), Average_Income = Income_Per_Person / Number_of_People), .(Number_of_People, Income_Per_Person)]``` – M-- Jan 28 '20 at 22:29

1 Answers1

2

We can use uncount

library(dplyr)
library(tidyr)
df1 %>%
  uncount(`Number of People`, .remove = FALSE)

By default, .remove = TRUE, i.e. it will remove the 'Number of People' column in the output

akrun
  • 874,273
  • 37
  • 540
  • 662