0

I have a df with unique rows

df  <- data.frame(id = c("A","B","C"), D = c(1,0,1), E = c(1,0,1), F = c(1,1,0))

another dataframe has not unique values like the following.

df2  <- data.frame(id = c("A","A","A","B","B","C"))

The key between these two dataframes is the id columns.

Is there any possible way to melt/merge this to dataframes into one like the following output?

dfinal  <- data.frame(id = c("A","A","A","B","B","C"), D = c(1,1,1,0,0,1), E = c(1,1,1,0,0,1), F = c(1,1,1,1,1,0))
> 
> dfinal
  id A B C
1  A 1 1 1
2  A 1 1 1
3  A 1 1 1
4  B 0 0 1
5  B 0 0 1
6  C 1 1 0
PitterJe
  • 216
  • 2
  • 12

1 Answers1

1

just use the merge function:

merge(df2,df)
  id D E F
1  A 1 1 1
2  A 1 1 1
3  A 1 1 1
4  B 0 0 1
5  B 0 0 1
6  C 1 1 0
Onyambu
  • 67,392
  • 3
  • 24
  • 53