I have two data frames as follows :
df1 <- t(data.frame(seq(1,6,by=1),seq(6,1,by=-1)))
colnames(df1) <- c("A","B","C","D","E","F)
rownames(df1) <- c("a","b")
df2 <- data.frame(rep(colnames(df1),2),rep(rownames(df1),6))
colnames(df2) <- c("Vector1","Vector2")
Such that
df1
A B C D E F
a 1 2 3 4 5 6
b 6 5 4 3 2 1
df2
Vector1 Vector2
A a
B b
C a
D b
E a
F b
A a
B b
C a
D b
E a
F b
I want to match the column values of df2 to column names and row names of df1, and fill the corresponding value to a new column in df2 as follows:
Vector1 Vector2 Newcol
A a 1
B b 5
C a 3
D b 3
E a 5
F b 1
A a 1
B b 5
C a 3
D b 3
E a 5
F b 1
Any suggestions would be much appreciated. Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…