我的代码和数据类似于以下示例(在R中),但在下面的代码中,cbind命令未在for循环中实现,并且最终t1未添加到A,因为列数是不相等。请帮我。
test <- data.frame(c(2, 3),
c("b", "a"),
c("f", "t"),
c("T", "F"))
names(test) <- c("x", "y", "d", "L")
A <- data.frame(c(1, 2, 3),
c("a", "b", "c"),
c("f", "f", "t"),
c("T", "T", "F"),
c(2, 2, 3))
names(A) <- c("x", "y", "d", "L", "n")
for (i in 1:nrow(test)) {
t1 <- test[i, ]
if (t1$d == "f") {
A_reduct <- A[, c(1:2)]
test_reduct <- test[, c(1:2)]
mix_test_A <- rbind(A_reduct, test_reduct)
dist_mix_test_A <- as.matrix(FD::gowdis(mix_test_A))
idxs_mix_test_A <- KernelKnn::distMat.knn.index.dist(
dist_mix_test_A,
TEST_indices = c(4:5),
k = 1,
threads = 1,
minimize = T
)
nn_test_A <- idxs_mix_test_A$test_knn_idx
x <- nn_test_A[i]
lbltest <- test[i, 4]
lblgroup <- A[x, 4]
if (lbltest == lblgroup) {
y <- A[x, 5]
t1 <- cbind(t1, n = y)
A <- rbind(A, t1)
}
}
}