我有一个数据框,它是“两个方向”上 a、b 和 c 的所有可能排列
df1<-data.frame("x"=c("a","a","b"),"y"=c("b","c","c"),"A"=1:3 ,"B"=4:6,"C"=0,"T"=10:12)
df2<-data.frame("x"=df1$y,"y"=df1$x, "A"=df1$A,"B"=df1$B,"C"=df1$C,"T"=df1$T)
df<-rbind(df1,df2)
x y A B C T
1 a b 1 4 0 10
2 a c 2 5 0 11
3 b c 3 6 0 12
4 b a 1 4 0 10
5 c a 2 5 0 11
6 c b 3 6 0 12
我想用它来填充第二个空数据框
empty<-data.frame("x"=c("a","c"),"y"=c("b","a"),"A"=0,"T"=0)
x y A T
1 a b 0 0
2 c a 0 0
从而产生:
filled<-data.frame("x"=c("a","c"),"y"=c("b","a"),"A"=1:2,"T"=10:11)
x y A T
1 a b 1 10
2 c a 2 11
我尝试了一个 for 循环但没有运气
for(i in 1:nrow(empty)
{
if("x" == df$x && "y" == df$y)
{
empty[i,"A"]<-df$A
empty[i,"T"]<-df$T
}
}
以及上一篇关于填充矩阵但没有成功的答案。任何意见是极大的赞赏。