1

I have two datasets like below.

df1=data.frame(o=c(1,1,1,1,2,2,2,2,3,3,3,3))
df2=data.frame(o=c(1,1,2,2,3,3), ratio=c(0.75,0.25,0.25,0.75,0.5,0.5),
               d=c('a','b','a','b','a','b'))

> df1
   o
1  1
2  1
3  1
4  1
5  2
6  2
7  2
8  2
9  3
10 3
11 3
12 3

> df2
  o ratio d
1 1  0.75 a
2 1  0.25 b
3 2  0.25 a
4 2  0.75 b
5 3  0.50 a
6 3  0.50 b

Values in column 'o' in both dataframes are the same values. I want to merge two dataframes by column 'o' in both dataframes and ratio in df2 like below(hand made).

> df_final
   o d
1  1 a
2  1 a
3  1 a
4  1 b
5  2 a
6  2 b
7  2 b
8  2 b
9  3 a
10 3 a
11 3 b
12 3 b
hslee
  • 25
  • 3
  • Does this answer your question? [How to join (merge) data frames (inner, outer, left, right)](https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right) – MrSmithGoesToWashington Feb 25 '21 at 10:50

1 Answers1

1

Here is one idea. We can join the data first, calculate the number for each letter and create list based on the length, and then unnest the data frame.

library(dplyr)
library(tidyr)

df3 <- df2 %>%
  left_join(df1 %>% count(o), by = "o") %>%
  mutate(n2 = n * ratio) %>%
  rowwise() %>%
  mutate(d2 = list(rep(d, n2))) %>%
  unnest(cols = "d2") %>%
  select(o, d = d2)
df3
# # A tibble: 12 x 2
#      o d    
#  <dbl> <chr>
#  1     1 a    
#  2     1 a    
#  3     1 a    
#  4     1 b    
#  5     2 a    
#  6     2 b    
#  7     2 b    
#  8     2 b    
#  9     3 a    
# 10     3 a    
# 11     3 b    
# 12     3 b 
www
  • 38,575
  • 12
  • 48
  • 84