I have a dataset A and a summary dataset B.
A<-data.frame(ID=rep(c(1,2),times=c(3,2)))
A
ID
1
1
1
2
2
B<-data.frame(ID=c(1,2),value=c(14,15))
B
ID value
1 14
2 15
I want to add a column in dataset A that has the values in B repeated for each ID, like this:
ID value
1 14
1 14
1 14
2 15
2 15
So far, I've been achieving this using this code:
library(dplyr)
count<-A %>% group_by(ID)%>% tally()
n<-A$n
val<-rep(B$value,times=n)
A$value<-val
But I'm sure there's an easier way. Any thoughts?