I will merge the below two df's in ascending order by time, non-duplicating. My goal is to also have two new variables.
df1
time freq
1 1.5 1
2 3.5 1
3 4.5 2
4 5.5 1
5 8.5 2
6 9.5 1
7 10.5 1
8 11.5 1
9 15.5 1
10 16.5 1
11 18.5 1
12 23.5 1
13 26.5 1
df2
time freq
1 0.5 6
2 2.5 2
3 3.5 1
4 6.5 1
5 15.5 1
Please help me with the code for creating the two new columns:
Where if the
freq
value corresponds to atime
indf1
, then a new variable (var1
) would record the associatedfreq
value, AND0
if no suchtime
value exists for df1.Where if the
freq
value corresponds to atime
indf2
, then a second new variable (var2
) would record thatfreq
value fromdf2
, AND0
if no suchtime
value exists fordf2
.
So I would have a table like this below:
time var1 var2
0.5 0 6
1.5 1 0
2.5 0 2
3.5 1 1
4.5 2 0
5.5 1 0
...