This question already has answers here:
converting a matrix to a list
(2 answers)
Closed 4 years ago.
Is it possible to convert a matrix contains 6 rows and 4 columns into 6 vectors,
each row will be a vector.
m = matrix( c(2, 4, 3, 1, 5, 7,4,8,9,4,5,0,2,5,7,6,1,8), nrow=6,ncol=3)
m
[,1] [,2] [,3]
[1,] 2 4 2
[2,] 4 8 5
[3,] 3 9 7
[4,] 1 4 6
[5,] 5 5 1
[6,] 7 0 8
One option is split by the row of matrix to create a list of 'n' vectors where 'n' is the number of rows of the original matrix
lst <- split(m, row(m))
NOTE: It is better to create a list instead of having many objects in the global environment. Also, it is not clear why this is needed
You would try this example you can get the idea.
> b <- matrix(1:20, nrow = 2, ncol = 10)
> sapply(1:ncol(b), function(i) paste(b[,i],collapse=","))
[1] "1,2" "3,4" "5,6" "7,8" "9,10" "11,12" "13,14" "15,16"
[9] "17,18" "19,20"
A solution with lapply:
lapply(as.data.frame(t(m)), c)
Related
This question already has answers here:
extracting a particular diagonal form a data frame [duplicate]
(5 answers)
Sum of antidiagonal of a matrix
(5 answers)
Closed 1 year ago.
I want to form a vector from some specific elements of matrix (or dataframe).
I would like to create vector from for example minor diagonal elements from this matrix (e.g elements matrix[3,1], matrix[2,2] and matrix [1,3]).
How could I do it without looping? My task is quite big and I would like skip looping. Following command:
matrix[c(3, 2, 1), c(1, 2, 3)]
instead of vector c(3, 5, 7) gives me another matrix.
Either of these will do what you want:
x <- matrix(1:9, 3, 3)
x
# [,1] [,2] [,3]
# [1,] 1 4 7
# [2,] 2 5 8
# [3,] 3 6 9
rc <- seq(ncol(x))
diag(x[rev(rc),])
# [1] 3 5 7
x[cbind(rev(rc), rc)]
# [1] 3 5 7
try this
mat <- matrix(1:9, nrow = 3, byrow = T)
diag(apply(mat,2,rev))
Your matrix:
mymatrix <- matrix(data= c(1,2,3,4,5,6,7,8,9), nrow = 3, byrow = T)
# [,1] [,2] [,3]
#[1,] 1 2 3
#[2,] 4 5 6
#[3,] 7 8 9
you can get the diagonal like this:
diag(mymatrix)
# [1] 1 5 9
Suppose you have a matrix a
a <- matrix(1:9, 3, 3)
a
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
and a vector b indicating which element of each row you want to extract. That is, the vector b indicates the column of the element, for instance:
b <- c(1, 3, 1)
If we want to extract the indicated data points, we can simply index each desired element like this:
a[cbind(1:nrow(a),b)]
[1] 1 8 3
I would like to do it with a negative index vector. That is, R should return a matrix where exactly one element per row is omitted (in this case, a 3x2 matrix). If I try it in a naive approach, R throws an error:
c = -b
a[cbind(1:nrow(a),c)]
Error in a[cbind(1:nrow(a), c)] :
negative values are not allowed in a matrix subscript
Thank you!
Not pretty, but you could do
b <- c(1, 3, 1) + 3 * 0:2
matrix(c(t(a))[-b], 3, 2, byrow = TRUE)
Maybe this is another naive approach. We loop over every row in the matrix and remove index specified in b.
t(sapply(seq_len(nrow(a)), function(x) a[x, -b[x]]))
# [,1] [,2]
#[1,] 4 7
#[2,] 2 5
#[3,] 6 9
Or using mapply with split
t(mapply(`[`, split(a, seq_len(nrow(a))), -b))
This question already has answers here:
Index values from a matrix using row, col indices
(4 answers)
Closed 4 years ago.
Say you have the following matrix:
mat <- matrix(1:12, nrow = 3, ncol = 4)
print(mat)
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
And you wish to extract values from the matrix with the following row and column indices:
rowind <- c(2,3,1)
colind <- c(4,4,1)
So I'm looking for the values 11 (row 2, col 4), 12 (row 3, col 4) and 1 (row 1, col 1).
If I try passing the column and row vectors into the matrix with:
mat[rowind, colind]
I get a new matrix where all permutations of rowind and colind are included:
[,1] [,2] [,3]
[1,] 11 11 2
[2,] 12 12 3
[3,] 10 10 1
How I can instead get the values relating only to the specific row and column combinations? I considered using a for loop but given my matrix and index vectors are large I feel this would be unnecessarily slow and that there is very likely a better way.
We can cbind the indices to extract the values
mat[cbind(rowind, colind)]
#[1] 11 12 1
This question already has answers here:
Convert a matrix to a 1 dimensional array
(11 answers)
Closed 7 years ago.
I want to extract all elements of a matrix and put them in a vector row-wise.
For example, if my matrix is:
[,1] [,2] [,3]
1 2 3
4 5 6
then, I want to have a vector like this:
[1, 2, 3, 4, 5, 6]
How should I do this in R?
Just use c(t(yourmatrix)):
m <- matrix(1:6, ncol = 3, byrow = TRUE)
m
# [,1] [,2] [,3]
# [1,] 1 2 3
# [2,] 4 5 6
c(t(m))
# [1] 1 2 3 4 5 6
This question already has answers here:
How to generate all possible combinations of vectors without caring for order?
(2 answers)
Closed 9 years ago.
Imagine I have a vector x and i'd like to create a matrix with all the possible n choose 2 combinations of the elements of x.
More in detail, let us say x is,
x = c(1,2,3,4)
Then, all the possible (4 choose 2) = 6,
X = as.matrix(data.frame(col1 = c(1,1,1,2,2,3), col2 = c(2,3,4,3,4,4)))
Is there a function in R to do that?
As pointed out by #Arun, you can use combn
> t(combn(x, 2))
[,1] [,2]
[1,] 1 2
[2,] 1 3
[3,] 1 4
[4,] 2 3
[5,] 2 4
[6,] 3 4