2

I have the following data frame:

  Id    Value Freq
1 A     8     2
2 B     7     3
3 C     2     4

and I want to obtain a new data frame by replicating each Value according to Freq:

  Id    Value
1 A     8    
2 A     8    
3 B     7    
4 B     7    
5 B     7    
6 C     2    
7 C     2    
8 C     2    
9 C     2    

I understand this can be very easily done with purrr (I have identified map_dfr as the most suitable function), but I cannot understand what is the best and most "compact" way to do it.

Massimo2013
  • 533
  • 4
  • 17

2 Answers2

1

You can just use some nice indexing-properties of dataframes.

df <- data.frame(Id=c("A","B","C"),Value=c(8,7,2),Freq=c(2,3,4))

replicatedDataframe <- do.call("rbind",lapply(1:NROW(df), function(k) {
  df[rep(k,df$Freq[k]),-3]
}))

This can be done more easier using the times-argument in rep:

replicatedDataframe  <- df[rep(1:NROW(df),times=df$Freq),-3]
Jonas
  • 1,760
  • 1
  • 3
  • 12
1

Convert Freq to a vector and unnest.

df %>%
  mutate(Freq = map(Freq, seq_len)) %>%
  unnest(Freq) %>%
  select(-Freq)
#> # A tibble: 9 x 2
#>   Id    Value
#>   <chr> <dbl>
#> 1 A         8
#> 2 A         8
#> 3 B         7
#> 4 B         7
#> 5 B         7
#> 6 C         2
#> 7 C         2
#> 8 C         2
#> 9 C         2
Paul
  • 8,734
  • 1
  • 26
  • 36