'Retaining matrix dimensions when performing lookup via character subsetting
I would like to use a named vector lookup to map the elements of a matrix to their integer index counterparts. I currently do the lookup via character subsetting but this drops the dimensions of the matrix. Specifying drop = FALSE does not help in this instance.
How can I return an elementwise-mapped matrix using a lookup whilst retaining the matrix dimensions, as per the desired output indicated?
cd3cd28 <- structure(c(26.4, 35.9, 59.4, 73, 33.7, 13.2, 16.5, 44.1, 82.8,
19.8, 8.82, 12.3, 14.6, 23.1, 5.19, 18.3, 16.8, 10.2, 13.5, 9.73,
58.8, 8.13, 13, 1.29, 24.8, 6.61, 18.6, 14.9, 5.83, 21.1, 17,
32.5, 32.5, 11.8, 46.1, 414, 352, 403, 528, 305, 17, 3.37, 11.4,
13.7, 4.66, 44.9, 16.5, 31.9, 28.6, 25.7, 40, 61.5, 19.5, 23.1,
81.3),
.Dim = c(5L, 11L), .Dimnames = list(NULL, c("praf", "pmek", "plcg",
"PIP2", "PIP3", "p44.42",
"pakts473", "PKA", "PKC",
"P38", "pjnk")))
protein_to_idx <- setNames(1:ncol(cd3cd28), colnames(cd3cd28))
proteins <- matrix(c("PKC", "PKA",
"PKA", "P38",
"PKA", "pjnk"),
ncol = 2)
protein_to_idx[proteins]
# Current output
# PKC PKA PKA P38 PKA pjnk
# 9 8 8 10 8 11
# Desired output
matrix(protein_to_idx[proteins], ncol = 2)
# [,1] [,2]
# [1,] 9 10
# [2,] 8 8
# [3,] 8 11
Note: Specifying drop = FALSE does not work here (single dimension indexing), i.e. the following
protein_to_idx[proteins, drop = FALSE]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
