通过对几个列值的条件子集矩阵(整数,数值)

时间:2018-06-28 11:32:06

标签: r matrix dplyr subset

我有一个很大的矩阵,需要根据列的值(纬度和经度)对它进行子集化。这是我尝试过的示例代码:

> a<-1:100
> a<-matrix(a,c(20,5))
> colnames(a)<-c("col1","col2","col3","col4","col5")
> b<-subset(a, "col2">28, select = c("col1","col2","col3","col4","col5"))
> all(a==b)
[1] TRUE

即它没有用。所以我也尝试过:

> library(dplyr)
> b<-select(filter(a, "col2">28), c("col1","col2","col3","col4","col5"))
Error in UseMethod("filter_") : 
  no applicable method for 'filter_' applied to an object of class "c('matrix', 'integer', 'numeric')"

请帮助。预先感谢。

2 个答案:

答案 0 :(得分:0)

如我所见,您想基于col2来对列进行子集化。您还需要在subset的第二个参数中用a[,colname]指定矩阵。

a <- 1:100
a <-matrix(a, c(20,5))
colnames(a) <- c("col1","col2","col3","col4","col5")
b <- subset(a, a[,"col2"] > 28, select = c("col1","col2","col3","col4","col5"))

如果要过滤一个以上的列,则可以使用&|(以及和或)。例如:

a <- 1:100
a <-matrix(a, c(20,5))
colnames(a) <- c("col1","col2","col3","col4","col5")
b <- subset(a, a[,"col2"] > 28 & a[,"col3"] < 58, select = c("col1","col2","col3","col4","col5"))

这里是一个介于中间条件的示例,该条件也可以与&|组合。

b <- subset(a, dplyr::between(a[,"col2"], 28, 30), select = c("col1","col2","col3","col4","col5"))

答案 1 :(得分:0)

如果我答对了,您希望基于a中的值对矩阵col2进行子集化。您可能会考虑为此使用[]的组合。

a[a[, "col2"] > 28, ] # values in col2 greater than 28
# output
      col1 col2 col3 col4 col5
 [1,]    9   29   49   69   89
 [2,]   10   30   50   70   90
 [3,]   11   31   51   71   91
 [4,]   12   32   52   72   92
 [5,]   13   33   53   73   93
 [6,]   14   34   54   74   94
 [7,]   15   35   55   75   95
 [8,]   16   36   56   76   96
 [9,]   17   37   57   77   97
[10,]   18   38   58   78   98
[11,]   19   39   59   79   99
[12,]   20   40   60   80  100

a[a[, "col2"] > 28 & a[, "col2"] <= 38, ] # values in col2 greater than 28 and less or equal to 38 (i.e. between 28 and 38)
# output
      col1 col2 col3 col4 col5
 [1,]    9   29   49   69   89
 [2,]   10   30   50   70   90
 [3,]   11   31   51   71   91
 [4,]   12   32   52   72   92
 [5,]   13   33   53   73   93
 [6,]   14   34   54   74   94
 [7,]   15   35   55   75   95
 [8,]   16   36   56   76   96
 [9,]   17   37   57   77   97
[10,]   18   38   58   78   98

请注意,filter()中的函数dplyr需要一个tbl对象作为参数。因此,如果要使用它,则需要先将矩阵a设置为小标题或数据帧。 库(dplyr)

filter(as_tibble(a), col2 > 28)
filter(as.data.frame(a), col2 > 28)