r-从两个矩阵中选择不匹配的行



如何从一个矩阵中选择与另一个矩阵的行不匹配的行。这种情况是,我想在我的数据样本上训练一个模型,并在数据的其他部分上进行验证。提前谢谢。

您可以为此使用索引(正如Thomas所暗示的)。假设你有一个2000行的矩阵,并想随机选择其中的一半:

# Create the matrix
my.matrix <- matrix(rnorm(4000),nrow = 2000)
# Create a vector of 1000 row numbers
selection <- sample(1:2000, size = 1000)
# Create the 2 mutually exclusive matrices
matrix.1 <- my.matrix[selection,]
matrix.2 <- my.matrix[-selection,]

最新更新