Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I separate a matrix into smaller ones in R?

Tags:

r

matrix

I have the following matrix

2    4    1
6    32   1
4    2    1
5    3    2
4    2    2

I want to make the following two matrices based on 3rd column

first

2    4
6    32
4    2

second

5    3
4    2

Best I can come up with, but I get an error

x <- cbind(mat[,1], mat[,2]) if mat[,3]=1

y <- cbind(mat[,1], mat[,2]) if mat[,3]=2

like image 327
Steve Hwang Avatar asked Oct 25 '12 00:10

Steve Hwang


People also ask

How do you split a matrix in R?

In this example to Convert a given matrix to a list of column vectors in R we use the split() function. The split() function is used to split the data according to our requirement. In nrow() function we give the data as an argument. “each = nrow()” is used to repeat this process for each row.

Can you split a matrix?

For matrices, there is no such thing as division. You can add, subtract, and multiply matrices, but you cannot divide them.

How do I extract multiple columns from a matrix in R?

To pick out single or multiple columns use the select() function. The select() function expects a dataframe as it's first input ('argument', in R language), followed by the names of the columns you want to extract with a comma between each name.

How do you make a matrix of all ones in R?

In R, you create an all-ones matrix with the matrix() function. This basic R function requires 3 arguments, namely the value (i.e., the number 1), the number of rows, and the number of columns. You can use the matrix() function to create square and non-square all-ones matrices.


4 Answers

If mat is your matrix:

mat <- matrix(1:15,ncol=3)
mat[,3] <- c(1,1,1,2,2)
> mat
     [,1] [,2] [,3]
[1,]    1    6    1
[2,]    2    7    1
[3,]    3    8    1
[4,]    4    9    2
[5,]    5   10    2

Then you can use split:

> lapply( split( mat[,1:2], mat[,3] ), matrix, ncol=2)
$`1`
     [,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8

$`2`
     [,1] [,2]
[1,]    4    9
[2,]    5   10

The lapply of matrix is necessary because split drops the attributes that make a vector a matrix, so you need to add them back in.

like image 100
Ari B. Friedman Avatar answered Oct 22 '22 07:10

Ari B. Friedman


Yet another example:

#test data
mat <- matrix(1:15,ncol=3)
mat[,3] <- c(1,1,1,2,2)

#make a list storing a matrix for each id as components
result <- lapply(by(mat,mat[,3],identity),as.matrix)

Final product:

> result
$`1`
  V1 V2 V3
1  1  6  1
2  2  7  1
3  3  8  1

$`2`
  V1 V2 V3
4  4  9  2
5  5 10  2
like image 27
thelatemail Avatar answered Oct 22 '22 05:10

thelatemail


If you have a matrix A, this will get the first two columns when the third column is 1:

A[A[,3] == 1,c(1,2)]

You can use this to obtain matrices for any value in the third column.

Explanation: A[,3] == 1 returns a vector of booleans, where the i-th position is TRUE if A[i,3] is 1. This vector of booleans can be used to index into a matrix to extract the rows we want.

Disclaimer: I have very little experience with R, this is the MATLAB-ish way to do it.

like image 30
pedrosorio Avatar answered Oct 22 '22 05:10

pedrosorio


This is a functional version of pedrosorio's idea:

 getthird <- function(mat, idx) mat[mat[,3]==idx, 1:2]
 sapply(unique(mat[,3]), getthird, mat=mat)  #idx gets sent the unique values
#-----------
[[1]]
     [,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8

[[2]]
     [,1] [,2]
[1,]    4    9
[2,]    5   10
like image 27
IRTFM Avatar answered Oct 22 '22 07:10

IRTFM