I am trying to merge two dataframe into a long format dataframe based on two variables "time" and "ID"
df1<-data.frame(id=c(1,2,3), time=c(1,1,1), var=(1,0,1), var1=(1,0,1)
df2<-data.frame(id=c(1,2,3), time=c(2,2,2), var=(0,1,0))
So that the end result of the merged dataframe will look like this
time | ID | var | var1 |
---|---|---|---|
1 | 1 | 1 | 1 |
1 | 2 | 0 | 0 |
1 | 3 | 1 | 1 |
2 | 1 | 0 | |
2 | 2 | 1 | |
2 | 3 | 0 |
Please let me know how I could do it in r, much appreciated. Thanks!
I tried using the left_join command,
x<-left_join(df1,df2,by=c("ID","time"))
but it gives me an error message as follows:
Error in left_join()
:
! Join columns in y
must be present in the data.
✖ Problem with time
.
I tried using rbind:
x<- rbind(df1,df2)
but I get the following error message below: Error in rbind(deparse.level, ...) : numbers of columns of arguments do not match