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

Disaggregate Rows according to the value of a column in R

Given df1, I need to expand the same amount of rows according to the value given by the TRIP column. As an example I put two columns but in real life it is a dataframe with more than 15 columns. How do I expand all records from the value recorded in the Trip column?


*df1*       
        
Id  Trip
1   3
2   2
3   2
4   4


Expected Result

*df1*       
        
Id  Trip
1   1
1   2
1   3
2   1
2   2
3   1
3   2
4   1
4   2
4   3
4   4

>Solution :

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

We can do

library(dplyr)
library(tidyr)
df1 %>%
   uncount(Trip, .remove = FALSE) %>%
   group_by(Id) %>%
   mutate(Trip = row_number()) %>%
   ungroup

-output

# A tibble: 11 × 2
      Id  Trip
   <int> <int>
 1     1     1
 2     1     2
 3     1     3
 4     2     1
 5     2     2
 6     3     1
 7     3     2
 8     4     1
 9     4     2
10     4     3
11     4     4

Or another option is

library(purrr)
df1 %>%
   mutate(Trip = map(Trip, seq_len)) %>%
   unnest(Trip)

-output

# A tibble: 11 × 2
      Id  Trip
   <int> <int>
 1     1     1
 2     1     2
 3     1     3
 4     2     1
 5     2     2
 6     3     1
 7     3     2
 8     4     1
 9     4     2
10     4     3
11     4     4

data

df1 <- structure(list(Id = 1:4, Trip = c(3L, 2L, 2L, 4L)), 
class = "data.frame", row.names = c(NA, 
-4L))
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