首页 > 解决方案 > 如何根据 R 中的行和将矩阵一分为二?

问题描述

如果我有这个矩阵,MatrixA:

        sample1 sample2 sample3
red 0   0   1
blue    47  39  44
green   18  109 6
orange  4   78  1000

我想根据行总和创建 2 个单独的矩阵。MatrixB 将具有行总和 >= 100 的所有行,而 MatrixC 将具有行总和 < 100 的所有行。

我已经尝试使用 rowSums 进行多次迭代,但是我要么得到 MatrixB 和 MatrixC 完全相同的矩阵,要么得到的 MatrixC 比 MatrixB 少 100 行。我的实际数据有 >10 万行。

keep <- rowSums(MatrixA)>= 100
remove <- rowSums(MatrixA)< 100
MatrixB <- MatrixA[keep,]
MatrixC <- MatrixA[!!keep,]

如何创建两个看起来像这样的最终矩阵?

MatrixB

    sample1 sample2 sample3
blue    47  39  44
green   18  109 6
orange  4   78  1000

MatrixC

    sample1 sample2 sample3
red 0   0   1

标签: r

解决方案


试试这个解决方案。I您可以根据所需的行和和阈值创建索引变量。之后,您可以使用split()并将结果存储在列表中。这样,您将获得所需的输出:

#Code
#Create rowsums
df$I <- ifelse(rowSums(df[,-1])>=100,'Keep','Remove')
#Now split
List <- split(df,df$I)
#Remove I var
List <- lapply(List,function(x) {x$I<-NULL;return(x)})

输出:

List
$Keep
     var sample1 sample2 sample3
2   blue      47      39      44
3  green      18     109       6
4 orange       4      78    1000

$Remove
  var sample1 sample2 sample3
1 red       0       0       1

使用的一些数据:

#Data
df <- structure(list(var = c("red", "blue", "green", "orange"), sample1 = c(0L, 
47L, 18L, 4L), sample2 = c(0L, 39L, 109L, 78L), sample3 = c(1L, 
44L, 6L, 1000L), I = c("Remove", "Keep", "Keep", "Keep")), row.names = c(NA, 
-4L), class = "data.frame")

如果要将数据帧放入环境,可以使用下一个代码:

#Code
list2env(List,envir = .GlobalEnv)

它将使用名称KeepRemove.

现在你有一个矩阵的情况下,你应该使用下一个代码:

#Create index
index <- which(rowSums(mat)>=100)
#Create matrices
m1 <- mat[index,]
m2 <- mat[-index,,drop=F]
m1
m2

输出:

m1
       sample1 sample2 sample3
blue        47      39      44
green       18     109       6
orange       4      78    1000

m2
    sample1 sample2 sample3
red       0       0       1

使用的矩阵:

#Matrix
mat <- structure(c(0L, 47L, 18L, 4L, 0L, 39L, 109L, 78L, 1L, 44L, 6L, 
1000L), .Dim = 4:3, .Dimnames = list(c("red", "blue", "green", 
"orange"), c("sample1", "sample2", "sample3")))

推荐阅读