How to Subset a Matrix with Different Column Positions for Each Row

How to subset a matrix with different column positions for each row?

We can try the row/column indexing

M[cbind(1:nrow(M), v)]
#[1] 11 2 3 9 5

Subset a data.frame based on row-column combinations

Convert it into matrix and subset

df[as.matrix(sub)]
#[1] -1.25290762 0.09182166

Subset a matrix according to a columns vector

Try

H[cbind(seq_len(nrow(H)), P)]
## [1] 0.6733731 0.7396847 0.5953580

Here we are indexing by consecutive rows and columns indicated in P


Regarding your question, so the reason H[, P] returns a matrix is because you are telling R:

select all rows in columns: 2, 1, 2 from matrix "H"

thus the result that you are getting is a matrix with identical first and third columns.

R // subset matrix rows and columns based on names

You may also subset for names.

y <- x[c("a", "c"), c("a", "c")]
y
# a c
# a 2 5
# c 4 7

Or, using subset

y <- subset(x, colnames(x) %in% c("a", "c"), 
rownames(x) %in% c("a", "c"))
y
# a c
# a 2 5
# c 4 7

How to subset and reorder a matrix based on the row names of other matrix

You will get a lot more help if you share reproducible examples. You can use the dput( ) function to help share your data.

you can subset with this code:

marker[row.names(marker) %in% row.names(pheno),]

Reorder columns of matrix, with each row taking on a different order

I seem to get it running by doing a round trip through converting to numerics first and then re-creating a matrix:

datExprNorm_numeric <- as.numeric(datExprNorm)
inds_numeric <- as.numeric(inds)

sampled <- datExprNorm_numeric[inds_numeric]

datPermut <- matrix(sampled, nrow = nrow(datExprNorm), ncol = ncol(datExprNorm))

Here's a smaller toy example:

x <- matrix(c(4, 3, 2, 1), nrow = 2)

gives:

     [,1] [,2]
[1,] 4 2
[2,] 3 1

and

y <- matrix(c(1, 1, 2, 2), nrow = 2)

gives

     [,1] [,2]
[1,] 1 2
[2,] 1 2

now combine:

z <- as.numeric(x)[as.numeric(y)]

z <- matrix(z, nrow = 2, ncol = 2)

result:

     [,1] [,2]
[1,] 4 3
[2,] 4 3

How would I create a subset by matching multiple patterns at a specific location in column names?

We could use a combination of str_locate and which to select columns. If you have a list of search terms, then those can be collapsed into one list with paste0. Then, we can locate the search terms at particular positions (i.e., 11 and 12), and select those columns.

library(tidyverse)

key_chr <- c("JG", "HB", "KU")
search_terms <- paste0(key_chr, collapse = "|")

df %>%
select(which(str_locate(names(df), search_terms)[,1] == 11 & str_locate(names(df), search_terms)[,2] == 12))

Or in base R, we could write it as:

df <- df[, which(regexpr(search_terms, names(df)) == 11)]

Output

           TCGA.OR.A5JG.01A TCGA.PK.A5HB.01A TCGA.OR.A5KU.01A
cg00000029 0.9091428 0.8603163 0.08972934
cg00000108 NA NA NA
cg00000109 NA NA NA
cg00000165 0.8705515 0.2839199 0.16676025
cg00000236 0.9170243 0.9235076 0.92036744

Subsetting one matrix by another matrix

You can extract the TRUE columns in each row of A first, then for each of those columns, extract that column from B

lapply(apply(A, 1, which), function(i) B[,i,drop=FALSE])

Select a column by column-name, but a different name for each row of a matrix in R?

As the help page ?`[` says, you can subset with a matrix to get individual elements. Each row of the subsetting matrix is an element, and the columns specify the indices for each dimension.

match(columns,colnames(aMatrix)) #gets the column indices
# [1] 1 5 4
b <- cbind(seq_along(columns),match(columns,colnames(aMatrix))) #subset matrix
# [,1] [,2]
# [1,] 1 1 #first element: first row first column
# [2,] 2 5 #second element: second row fifth column
# [3,] 3 4 #third element: third row fourth column
aMatrix[b]
# [1] 1 14 12


Related Topics



Leave a reply



Submit