2

I have a matrix like this in R:

          [,1]        [,2]        [,3]         [,4]            [,5]   
19992    -33.54971    23.35746    0.0000000    2.107680e+01    19980219
19993    -33.54203    23.40079    0.0000000    2.107696e+01    19980219
19994    -33.53453    23.44445    0.0000000    2.107713e+01    19980219
19995    -33.52719    23.48840    0.0000000    2.107730e+01    19980219
19996    -33.51965    23.53200    0.0000000    2.107746e+01    19980219
19997    -33.51183    23.57565    0.0000000    2.107763e+01    19980219
19998    -33.50446    23.61958    0.0000000    2.107780e+01    19980219
19999    -33.49678    23.66313    0.0000000    2.107796e+01    19980219 

Its actually a lot bigger (2.000.000 rows) but I think that this example will do for my question.

I want to extract all rows that have a value between e.g. -33.52... and -33.55... in the first column and create a new matrix of these extracted rows.

The output matrix would than be for example:

19992    -33.54971    23.35746    0.0000000    2.107680e+01    19980219
19993    -33.54203    23.40079    0.0000000    2.107696e+01    19980219
19994    -33.53453    23.44445    0.0000000    2.107713e+01    19980219
19995    -33.52719    23.48840    0.0000000    2.107730e+01    19980219

Some tips would be great!

4

3 回答 3

2

假设您的原始矩阵称为the_matrix. 要获取第一列中的值在指定范围内的那些行索引,您可以使用

ind <- which( the_matrix[, 1] < -33.52 & the_matrix[, 1] > -33.55 )

要创建仅包含这些行的新矩阵,请使用

new_matrix <- the_matrix[ind, ]
于 2013-04-25T13:49:10.530 回答
2

使用可用于子集的逻辑比较,如下所示:

set.seed(1096)
m <- matrix( runif(36,-30,30) , ncol = 6 )
m
            [,1]       [,2]      [,3]       [,4]       [,5]       [,6]
[1,] -10.8645147  12.701539 11.830714  12.027012  13.007876 -27.569211
[2,] -27.7127809 -20.907217 26.667036 -16.142388  20.287297  12.283898
[3,]   8.1607388  13.326040 -8.937501   2.541782   2.705917  18.490514
[4,]  14.0729963   4.522238 13.869731  13.708621 -22.996189  29.276250
[5,]  -0.3717591  18.057403 18.940814   3.430272 -23.460082 -27.296111
[6,] -14.0079398  -7.932984 24.836415 -20.442637  20.630987   8.138304

m[ m[,1] > -15 & m[,1] < -10 , ]
          [,1]      [,2]     [,3]      [,4]     [,5]       [,6]
[1,] -10.86451 12.701539 11.83071  12.02701 13.00788 -27.569211
[2,] -14.00794 -7.932984 24.83641 -20.44264 20.63099   8.138304

我们将条件放在[操作符的行位置中(注意,上面最后一个命令中的条件之后,我们告诉 R 返回所有列)。

我们将条件添加到使用&运算符将​​条件链接在一起的子集中,因此在这种情况下,我们只希望第一列 ( m[,1]) 中的值大于 -15&小于 -10 的行。

你可能会说

m[ (some condition) , c(1,2) ]

if(some condition)是一个表达式,R 可以计算为其中一个TRUE,否则FALSE它将只返回计算结果为 的行TRUE。这次我们要求第 1 列和第 2 列(我们也可以说1:2

于 2013-04-25T13:49:34.577 回答
1

使用您的数据,我们可以做这样的事情

txt <- 
" -33.54971    23.35746    0.0000000    2.107680e+01    19980219
 -33.54203    23.40079    0.0000000    2.107696e+01    19980219
 -33.53453    23.44445    0.0000000    2.107713e+01    19980219
 -33.52719    23.48840    0.0000000    2.107730e+01    19980219
 -33.51965    23.53200    0.0000000    2.107746e+01    19980219
 -33.51183    23.57565    0.0000000    2.107763e+01    19980219
 -33.50446    23.61958    0.0000000    2.107780e+01    19980219
 -33.49678    23.66313    0.0000000    2.107796e+01    19980219"

mat <- matrix(scan(text = txt), ncol = 5, byrow = TRUE)
cond <- mat[,1] < -33.52 & mat[,1] > -33.55
mat[cond, ]

##         [,1]   [,2] [,3]   [,4]     [,5]
## [1,] -33.550 23.357    0 21.077 19980219
## [2,] -33.542 23.401    0 21.077 19980219
## [3,] -33.535 23.444    0 21.077 19980219
## [4,] -33.527 23.488    0 21.077 19980219
于 2013-04-25T13:51:07.010 回答