选择矩阵中满足条件的行

发布于 2024-10-24 08:31:37 字数 410 浏览 1 评论 0原文

在带有矩阵的 R 中:

     one two three four
 [1,]   1   6    11   16
 [2,]   2   7    12   17
 [3,]   3   8    11   18
 [4,]   4   9    11   19
 [5,]   5  10    15   20

我想提取其行具有第三列 = 11 的子矩阵。也就是说:

      one two three four
 [1,]   1   6    11   16
 [3,]   3   8    11   18
 [4,]   4   9    11   19

我想在不循环的情况下执行此操作。我是 R 新手,所以这可能非常明显,但是 文档通常比较简洁。

In R with a matrix:

     one two three four
 [1,]   1   6    11   16
 [2,]   2   7    12   17
 [3,]   3   8    11   18
 [4,]   4   9    11   19
 [5,]   5  10    15   20

I want to extract the submatrix whose rows have column three = 11. That is:

      one two three four
 [1,]   1   6    11   16
 [3,]   3   8    11   18
 [4,]   4   9    11   19

I want to do this without looping. I am new to R so this is probably very obvious but the
documentation is often somewhat terse.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

月竹挽风 2024-10-31 08:31:38
m <- matrix(1:20, ncol = 4) 
colnames(m) <- letters[1:4]

以下命令将选择上面矩阵的第一行。

subset(m, m[,4] == 16)

这将选择最后三个。

subset(m, m[,4] > 17)

在这两种情况下,结果都是矩阵。
如果您想使用列名称来选择列,那么您最好将其转换为数据框,

mf <- data.frame(m)

然后您可以选择,

mf[ mf$a == 16, ]

或者,您可以使用子集命令。

m <- matrix(1:20, ncol = 4) 
colnames(m) <- letters[1:4]

The following command will select the first row of the matrix above.

subset(m, m[,4] == 16)

And this will select the last three.

subset(m, m[,4] > 17)

The result will be a matrix in both cases.
If you want to use column names to select columns then you would be best off converting it to a dataframe with

mf <- data.frame(m)

Then you can select with

mf[ mf$a == 16, ]

Or, you could use the subset command.

疯了 2024-10-31 08:31:38

我将选择使用 dplyr 包的简单方法。

如果数据框是数据。

library(dplyr)
result <- filter(data, three == 11)

I will choose a simple approach using the dplyr package.

If the dataframe is data.

library(dplyr)
result <- filter(data, three == 11)
寒江雪… 2024-10-31 08:31:38

Subset是一个非常慢的函数,我个人觉得它没什么用。

我假设您有一个名为 Mat 的 data.frame、数组、矩阵,其中 ABC 作为列姓名;那么您需要做的就是:

  • 在一列上有一个条件的情况下,假设 A 列

    Mat[which(Mat[,'A'] == 10), ]
    

如果不同列上有多个条件,您可以创建一个虚拟变量。假设条件为 A = 10B = 5C > 1。 2,那么我们有:

    aux = which(Mat[,'A'] == 10)
    aux = aux[which(Mat[aux,'B'] == 5)]
    aux = aux[which(Mat[aux,'C'] > 2)]
    Mat[aux, ]

通过使用system.time测试速度优势,which方法比subset快10倍方法。

Subset is a very slow function , and I personally find it useless.

I assume you have a data.frame, array, matrix called Mat with A, B, C as column names; then all you need to do is:

  • In the case of one condition on one column, lets say column A

    Mat[which(Mat[,'A'] == 10), ]
    

In the case of multiple conditions on different column, you can create a dummy variable. Suppose the conditions are A = 10, B = 5, and C > 2, then we have:

    aux = which(Mat[,'A'] == 10)
    aux = aux[which(Mat[aux,'B'] == 5)]
    aux = aux[which(Mat[aux,'C'] > 2)]
    Mat[aux, ]

By testing the speed advantage with system.time, the which method is 10x faster than the subset method.

他是夢罘是命 2024-10-31 08:31:38

如果您的矩阵名为 m,只需使用:

R> m[m$three == 11, ]

If your matrix is called m, just use :

R> m[m$three == 11, ]
心凉怎暖 2024-10-31 08:31:38

如果数据集称为data,则所有行满足列“pm2.5”的值>“pm2.5”的条件。 300 可以通过 -

data[data['pm2.5'] >300,]

If the dataset is called data, then all the rows meeting a condition where value of column 'pm2.5' > 300 can be received by -

data[data['pm2.5'] >300,]
断念 2024-10-31 08:31:37

如果使用 as.data.frame() 将矩阵转换为数据框,这会更容易做到。在这种情况下,前面的答案(使用子集或 m$三)将起作用,否则将不起作用。

要对矩阵执行操作,您可以按名称定义列:

m[m[, "three"] == 11,]

或按数字定义列:

m[m[,3] == 11,]

请注意,如果只有一行匹配,则结果是整数向量,而不是矩阵。

This is easier to do if you convert your matrix to a data frame using as.data.frame(). In that case the previous answers (using subset or m$three) will work, otherwise they will not.

To perform the operation on a matrix, you can define a column by name:

m[m[, "three"] == 11,]

Or by number:

m[m[,3] == 11,]

Note that if only one row matches, the result is an integer vector, not a matrix.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文