In R creating vector from specific matrix (dataframe) elements [duplicate] - r

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

Related

How to multiply a matrix by a column in another data set? [duplicate]

This question already has answers here:
Fastest way to multiply matrix columns with vector elements in R
(6 answers)
Closed 3 years ago.
I am a beginner and now I faced a problem, I think it should have a very easy solution. Thank you for your help.
I have a matrix 313*442
each column should multiply with a fixed number in a separate column in the other data set.
Column one should multiply by 0.8, column two should multiply by -2.3 and ... and at the end, the sum of the row should calculate.
in final I should have one column that should correspond to each row.
An option would be to replicate the column in the second dataset to make the lengths same and multiply with the matrix
m1 * df1$v1[col(m1)]
# [,1] [,2]
#[1,] 2 9
#[2,] 4 12
Or another option is sweep
sweep(m1, 2, df1$v1, `*`)
# [,1] [,2]
#[1,] 2 9
#[2,] 4 12
data
m1 <- matrix(c(1, 2, 3, 4), ncol = 2)
df1 <- data.frame(v1 = c(2, 3))
Assuming that m1 is your matrix and v1 the vector with the values to be multiplied by each column. Then,
m1 <- matrix(c(1, 2, 3, 4), ncol = 2)
m1
[,1] [,2]
[1,] 1 3
[2,] 2 4
v1 <- c(2, 3)
t(t(m1) * v1)
# [,1] [,2]
#[1,] 2 9
#[2,] 4 12

How to extract specific values from Dataframe or Matrix using vectors of row and column indices? [duplicate]

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

Convert matrix into number of vectors in R [duplicate]

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)

How to use data from every row to create a matrix by loop

I have a data frame like
df<-data.frame(a=c(1,2,3),b=c(4,5,6),c=c(7,8,9),d=c(10,11,12))
a b c d
1 1 4 7 10
2 2 5 8 11
3 3 6 9 12
I want to use every row to create 3 (nrow(df)) 2*2 matrixes. 1st use 1,4,7,10, 2nd use 2,5,8,11, 3rd use 3,6,9,12. So that I can get 3 matrixes. Thank you.
We can use split to split up the dataset into list and use matrix
lapply(split.default(as.matrix(df), row(df)), matrix, 2)
If we need the matrix columns to be 1, 7 followed by 4, 10, use the byrow=TRUE
lapply(split.default(as.matrix(df), row(df)), matrix, 2, byrow=TRUE)
Or use apply with MARGIN = 1 and wrap it with list to get a list output
do.call("c", apply(df, 1, function(x) list(matrix(x, ncol=2))))
If we need a for loop, preassign a as a list with length equal to the number of rows of 'df'
a <- vector("list", nrow(df))
for(i in 1:nrow(df)){ a[[i]] <- matrix(unlist(df[i,]), ncol=2)}
a
Or if it can be stored as array
array(t(df), c(2, 2, 3))
Or using map:
m <- matrix(c(t(df)), ncol = 2, byrow = T)
p <- 2 # number of rows
Map(function(i,j) m[i:j,], seq(1,nrow(m),p), seq(p,nrow(m),p))
# [[1]]
# [,1] [,2]
# [1,] 1 4
# [2,] 7 10
# [[2]]
# [,1] [,2]
# [1,] 2 5
# [2,] 8 11
# [[3]]
# [,1] [,2]
# [1,] 3 6
# [2,] 9 12

How to convert matrix rows to elements of a list in R? [duplicate]

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

Resources