我试图预先将变量分配给矩阵,并且在每个循环中我将矩阵(1乘5)分配给矩阵中的一行(5乘5)。
以下是具体的表格和代码:
表1
name score address
Jason 8 10 Cornell Street
Lisa 7 110 Park Avenue
David 10 225 Fifth Avenue
表2
name phone#
David 323-4527
Kudrow 233-3618
码
match_result=matrix("empty",ncol=5,nrow=5)
match_1=table1[,1]
match_2=table2[,1]
match_split_1 <- str_split(match_1, pattern = " ")
match_split_2 <- str_split(match_2, pattern = " ")
k=1
for (i in 1:length(match_split_1))
{
for (j in 1:length(match_split_2))
{
if (identical(match_split_12[[i]],match_split_22[[j]]))
{
temp_result <- c(table1[i,], table2[j,])
match_result[k,] = t(as.matrix(temp_result))
k <- k+1
}
}
}
这里我检查了昏暗(t(as.matrix(temp_result)))返回[1] 1 5
暗淡(match_result [1,]) NULL
任何人都可以指出我应该如何分配,以便它不会返回 match_result [1,]出错:维数不正确
谢谢!