【发布时间】:2019-05-27 15:44:51
【问题描述】:
我遵循了一个关于 R 语言的教程 https://www.datamentor.io/r-programming/matrix/,关于如何在索引矩阵而不是向量后保留返回的矩阵。但是,它不起作用,返回矩阵的类仍然是一个向量
我尝试使用一个逗号,然后使用两个逗号,就像教程中显示的那样。
[1] 3 6 9
> class(x[x%%3==0])
[1] "integer"
> x[x%%3==0, drop=FALSE]
[1] 3 6 9
> class(x[x%%3==0, drop=FALSE])
[1] "integer"
> x[x%%3==0,, drop=FALSE]
Error in x[x%%3 == 0, , drop = FALSE] :
(subscript) logical subscript too long
> x[x%%3==0,,.drop=FALSE]
Error in x[x%%3 == 0, , .drop = FALSE] : incorrect number of dimensions
> x[x%%3==0,,drop=FALSE]
Error in x[x%%3 == 0, , drop = FALSE] :
(subscript) logical subscript too long
> class(x[x%%3==0, drop=FALSE])
[1] "integer"
类仍然是整数,而不是矩阵,这是 drop=FALSE 应该做的
【问题讨论】:
-
请注意,当您在矩阵/数据/帧上执行
x %%3 == 0时,输出是逻辑矩阵。您将其作为行索引传递 -
你需要
x[rowSums(x%%3==0) == 3, , drop = FALSE]