0

I would like to multiply records according to a value of a variable (n in my dabatase) from the original database. How can I do this in R?

This is my db:

    city Val n
1 London 4.0 5
2   Rome 2.5 6
3  Milan 3.0 4
4 Venice 2.0 5

This is what I would like to obtain:

     city Val
1  London 4.0
2  London 4.0
3  London 4.0
4  London 4.0
5  London 4.0
6    Rome 2.5
7    Rome 2.5
8    Rome 2.5
9    Rome 2.5
10   Rome 2.5
11   Rome 2.5
12  Milan 3.0
13  Milan 3.0
14  Milan 3.0
15  Milan 3.0
16 Venice 2.0
17 Venice 2.0
18 Venice 2.0
19 Venice 2.0
20 Venice 2.0
Marios
  • 26,333
  • 8
  • 32
  • 52
ArTu
  • 431
  • 4
  • 20

1 Answers1

-1

We can use uncount from tidyr which is basically the standard tidyverse approach

library(tidyr)
uncount(df1, n) %>%
    as_tibble()
# A tibble: 20 x 2
#   city     Val
#   <chr>  <dbl>
# 1 London   4  
# 2 London   4  
# 3 London   4  
# 4 London   4  
# 5 London   4  
# 6 Rome     2.5
# 7 Rome     2.5
# 8 Rome     2.5
# 9 Rome     2.5
#10 Rome     2.5
#11 Rome     2.5
#12 Milan    3  
#13 Milan    3  
#14 Milan    3  
#15 Milan    3  
#16 Venice   2  
#17 Venice   2  
#18 Venice   2  
#19 Venice   2  
#20 Venice   2  

Or using slice

library(dplyr)
df1 %>%
    slice(rep(row_number(), n))

Or using base R

df1[rep(seq_len(nrow(df1)), df1$n),1:2]

data

df1 <- structure(list(city = c("London", "Rome", "Milan", "Venice"), 
    Val = c(4, 2.5, 3, 2), n = c(5L, 6L, 4L, 5L)), 
   class = "data.frame", row.names = c("1", 
"2", "3", "4"))
akrun
  • 874,273
  • 37
  • 540
  • 662