0
我有A R數據框df_big
合併r dataframes
Candidate Status
A 1
B 10
C 12
D 15
E 25
等
我有第二個數據幀df_small
Candidate_1 Candidate_2
A C
B E
C D
我要合併df_small
和df_big
到得到df_final
看起來像
Candidate_1 Candidate_2 Status_1 Status_2
A C 1 12
B E 10 25
C D 12 15
我試過的東西效果
df_small_1 = merge(x=df_small,y = df_big,by.x = "Candidate_1",by.y="Candidate")
df_small_2 = merge(x=df_small,y = df_big,by.x = "Candidate_2",by.y="Candidate")
,但我不知道如何結合df_small_1
和df_small_2
到df_small
像'df_final =合併(X =合併(X = df_small,Y = df_big,by.x = 「Candidate_2」,by.y = 「候選人」 ),y = df_big,by.x =「Candidate_1」,by.y =「Candidate」)' – HubertL
剛剛重塑爲long形式比較容易:'library(tidyverse); df_small%>%gather(var,Candidate)%>%left_join(df_big)' – alistaire