Extract matrix column values by matrix column name
Yes. But place your "test" after the comma if you want the column...
> A <- matrix(sample(1:12,12,T),ncol=4)
> rownames(A) <- letters[1:3]
> colnames(A) <- letters[11:14]
> A[,"l"]
a b c
6 10 1
see also help(Extract)
Extract column names for each value in column in matrix in R
We may use paste
in a vectorized way by rep
licating the column names with col
index and then get the unique
after paste
ing with the values of the n.mat
(it is a data.frame
after as.data.frame
- so we used unlist
)
unique(paste(colnames(n.mat)[col(n.mat)], unlist(n.mat), sep="_"))
-output
[1] "V1_M" "V1_F" "V2_A" "V2_B" "V3_T" "V3_G" "V3_H" "V4_D"
[9] "V4_G" "V5_T" "V5_X" "V6_Y" "V6_J"
Or if need a loop, use Map
unlist(Map(function(x, y) unique(paste(y, x, sep = "_")),
unname(n.mat), names(n.mat)))
The apply
wouldn't work here as the names
for each column will be the row.names attribute (even if change the OP's code to names
)
Extracting row and column names with its value from matrix
You could use sig_values
to subset Pmatrix
cbind.data.frame(colIDs = colnames(Pmatrix)[sig_values[, 1]],
rowIDs = rownames(Pmatrix)[sig_values[, 2]],
values = Pmatrix[sig_values])
# colIDs rowIDs values
#1 MU101188 MU101188 1.000
#2 MU101188 MU101310 0.506
#3 MU101310 MU101310 1.000
#4 MU101326 MU101326 1.000
#5 MU10251 MU101326 0.806
#6 MU10251 MU10251 1.000
Extracting rows and columns of a matrix if row names and column names have a partial match
An easier option is to reshape to 'long' by converting to data.frame
from table
, and then subset
the rows based on the values of 'Var1' and 'Var2'
out <- subset(as.data.frame.table(a), Var1 == sub("\\d+", "", Var2),
select =c(Var2, Freq))
with(out, setNames(Freq, Var2))
aaa1 aaa2 aaa3 bbb1 bbb2 bbb3 ccc1 ccc2 ccc3
0.01495641 1.57504185 2.32762287 0.42652979 0.41329383 0.07119408 0.64530516 1.39629918 0.17042160
Or with row/column
indexing
i1 <- match( sub("\\d+", "", colnames(a)), rownames(a))
a[cbind(i1, seq_along(i1))]
[1] 0.01495641 1.57504185 2.32762287 0.42652979 0.41329383 0.07119408 0.64530516 1.39629918 0.17042160
Extract multiple columns from matrix by column-name
Up front: dplyr::select
works on frames, not matrices. Fortunately, subset
has an S3 method, subset.matrix
:
subset(df, TRUE, c(A:C, E))
# A B C E
# A 0 1 19.92053 56
# B 1 2 19.94818 56
# C 2 3 19.97584 56
# D 3 4 20.00349 56
Alternatively, you can "subtract" columns as well.
subset(df, TRUE, -D)
# A B C E F G
# A 0 1 19.92053 56 NA NA
# B 1 2 19.94818 56 0.5295139 NA
# C 2 3 19.97584 56 0.4771412 32
# D 3 4 20.00349 56 0.4412616 32
Extract matrix column with it's name
We can use drop = FALSE
without converting to data.frame
m1[,3, drop = FALSE]
# Col3
#Days 9
#Amount 3200
Extracting col/row names from a matrix based on value condition in R
If we are interested in the row/column names, then convert to table
, and coerce it to a data.frame
and subset
subset(as.data.frame(as.table(m)), Freq > 5, select = c(Var1, Var2))
data
m <-structure(c(0, 1.6898, 7.55815, 4.18765, 4.4806, 4.41775, 3.9795,
4.1283, 4.255, 4.4811, 1.6898, 0, 7.67225, 4.113, 4.48225, 4.62525,
3.9288, 4.02495, 4.19675, 4.4686, 7.55815, 7.67225, 0, 7.3129,
7.23675, 7.46935, 7.29925, 7.41055, 7.4329, 7.28585, 4.18765,
4.113, 7.3129, 0, 3.8151, 3.35225, 2.886, 3.29, 3.0194, 3.949,
4.4806, 4.48225, 7.23675, 3.8151, 0, 4.2949, 3.66205, 4.0022,
3.70005, 2.34825, 4.41775, 4.62525, 7.46935, 3.35225, 4.2949,
0, 3.42355, 3.6388, 2.27245, 4.23745, 3.9795, 3.9288, 7.29925,
2.886, 3.66205, 3.42355, 0, 2.48115, 2.97045, 3.6137, 4.1283,
4.02495, 7.41055, 3.29, 4.0022, 3.6388, 2.48115, 0, 2.9763, 3.92015,
4.255, 4.19675, 7.4329, 3.0194, 3.70005, 2.27245, 2.97045, 2.9763,
0, 3.80345, 4.4811, 4.4686, 7.28585, 3.949, 2.34825, 4.23745,
3.6137, 3.92015, 3.80345, 0), .Dim = c(10L, 10L), .Dimnames = list(
c("0", "1", "2", "4", "5", "6", "7", "8", "9", "10"), c("0",
"1", "2", "4", "5", "6", "7", "8", "9", "10")))
Returning Column Name from Matrix based on value in R
1
Run sapply
over columns and find min
and then check if the min
meets your condition.
colnames(DF)[sapply(DF, min) <= 2]
#[1] "V3"
2
You can also run apply
on columns (MARGIN = 2
) to see if any
value in each column meets the required condition
colnames(DF)[apply(DF, MARGIN = 2, function(a) any(a<=2))]
#[1] "V3"
3
Use arr.ind = TRUE
with which
. This will give the indices of the values that meet the condition of which
. From that, extract the column information [,2]
.
unique(colnames(DF)[which(DF<=2, arr.ind = TRUE)[,2]])
#[1] "V3"
DATA
set.seed(42)
DF <- matrix(sample(1:9,9),ncol=3,nrow=3)
DF <- as.data.frame.matrix(DF)
DF
# V1 V2 V3
#1 9 5 6
#2 8 4 1
#3 3 7 2
extract column names from matrix
In your case, p1
is a vector, so you could use names
instead of colnames
. This should work:
cnames = names(p1)[inds[1]]
How do I retrieve a matrix column and row name by a matrix index value?
First you need to get the row and column of that index using arrayInd
.
k <- arrayInd(4, dim(mdat))
You can then get the right name by getting that element of the row and column names
rownames(mdat)[k[,1]]
colnames(mdat)[k[,2]]
Or both at once using mapply
:
mapply(`[[`, dimnames(mdat), k)
Related Topics
Why Would R Use the "L" Suffix to Denote an Integer
How to Add a Cumulative Column to an R Dataframe Using Dplyr
Performing Dplyr Mutate on Subset of Columns
How to Display All X Labels in R Barplot
Meaning of Ddply Error: 'Names' Attribute [9] Must Be the Same Length as the Vector [1]
What's the Difference Between '1L' and '1'
Convert a Numeric Month to a Month Abbreviation
Cut Function in R- Labeling Without Scientific Notations for Use in Ggplot2
How to Efficiently Use Rprof in R
Use Different Center Than the Prime Meridian in Plotting a World Map
Is There a More Elegant Way to Convert Two-Digit Years to Four-Digit Years with Lubridate
Why Does Unlist() Kill Dates in R
Calculate Row-Wise Proportions
Re-Ordering Factor Levels in Data Frame
Combine Rows in Data Frame Containing Na to Make Complete Row
What's the Best Way to Use R Scripts on the Command Line (Terminal)