我有两个具有不同行号的数据帧,但对应于相同的ID。
df1 <-
ID Day sign
1 1 -3 positive
2 1 -2 positive
3 1 -1 positive
4 1 0 positive
5 1 1 positive
6 2 -4 positive
7 2 -3 positive
8 2 -2 positive
9 2 -1 ULD
10 2 0 ULD
11 3 -2 positive
12 3 -1 positive
13 3 0 positive
14 3 1 positive
.. .. .. ..... .....
df2 <-
ID Type
1 1 secondary
2 1 secondary
3 2 primary
4 2 primary
5 3 secondary
6 3 secondary
我想在df1中添加一个称为“类型”的列,其条目(对于每个StudyNo)对应于df2中给出的“类型”。我特别希望结果是:
df1 <-
ID Day sign Type
1 1 -3 positive secondary
2 1 -2 positive secondary
3 1 -1 positive secondary
4 1 0 positive secondary
5 1 1 positive secondary
6 2 -4 positive primary
7 2 -3 positive primary
8 2 -2 positive primary
9 2 -1 ULD primary
10 2 0 ULD primary
11 3 -2 positive secondary
12 3 -1 positive secondary
13 3 0 positive secondary
14 3 1 positive secondary
.. .. .. ..... .....
我正在使用dplyr并尝试过
df3 <- left_join(df1, df2, by ="ID")
,但是结果表有重复的条目:例如ID = 1的10个条目。 像df3一样对获得所需输出的任何帮助,都表示感谢