RDGuida
RDGuida

Reputation: 566

How to use a list of values as indexes on a matrix?

I have a simple matrix (accessed through X2[,take[conval]] because it's a subset of an original one) made like this

Names             ColA                  ColB                ColC
338               0.03125               0.31250             0.03125
339               0.03125               0.31250             0.03125
518               0.03125               0.43750             0.06250
561               0.03125               0.03125             0.68750
562               0.03125               0.03125             0.68750

I have then created a list "indexes" with all the indexes I need to retain

$colA
338 339 518 561 562 
  1   2   3   4   5 

$colB
561 562 
  4   5 

$colC
338 339 
  1   2 

I need now to select just the values pointed by the inxes for further calculations. I do not want to use another for loop since I am already inside one and the program will be likely to run very slowly. I tried to implement something like

X2[,take[conval]][indexes]

or

sapply(X2[,take[conval]],"[",indexes)

But obviously it does not work since indexes is a list. Long story short I need to select all the elements from the first column,last two from the second and first two from the third without a for loop. Any ideas?

Upvotes: 2

Views: 104

Answers (2)

akrun
akrun

Reputation: 886938

Try

Map(`[`, X2[-1], indexes)
#$ColA
#[1] 0.03125 0.03125 0.03125 0.03125 0.03125

#$ColB
#[1] 0.03125 0.03125

#$ColC
#[1] 0.03125 0.03125

NOTE: I assumed that the dataset is data.frame. If it is a matrix, then convert to data.frame

Map(`[`, as.data.frame(X2[,-1]), indexes)

If you wish to get the elements in a vector, another option is

 X2[,-1][as.matrix(transform(stack(indexes), 
                ind=as.numeric(factor(ind))))]
 #[1] 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125

data

X2 <- structure(list(Names = c(338L, 339L, 518L, 561L, 562L),
ColA = c(0.03125, 
0.03125, 0.03125, 0.03125, 0.03125), ColB = c(0.3125, 0.3125, 
0.4375, 0.03125, 0.03125), ColC = c(0.03125, 0.03125, 0.0625, 
0.6875, 0.6875)), .Names = c("Names", "ColA", "ColB", "ColC"), 
class = "data.frame", row.names = c(NA, -5L))

indexes <- structure(list(colA = structure(1:5,
.Names = c("338", "339", 
"518", "561", "562")), colB = structure(c(4, 5), .Names = c("561", 
"562")), colC = structure(c(1, 2), .Names = c("338", "339"))),
.Names = c("colA", "colB", "colC"))

Upvotes: 3

rawr
rawr

Reputation: 20811

Maybe try this

dd <- read.table(header = TRUE, text="Names             ColA                  ColB                ColC
338               0.03125               0.31250             0.03125
339               0.03125               0.31250             0.03125
518               0.03125               0.43750             0.06250
561               0.03125               0.03125             0.68750
562               0.03125               0.03125             0.68750")

mm <- as.matrix(dd)
l <- list(ColA = 1:5, ColB = 4:5, ColC = 1:2)

lapply(names(l), function(x) mm[l[[x]], x])

# [[1]]
# [1] 0.03125 0.03125 0.03125 0.03125 0.03125
# 
# [[2]]
# [1] 0.03125 0.03125
# 
# [[3]]
# [1] 0.03125 0.03125

Upvotes: 3

Related Questions