Maybe this does what you want:

> dm <- cbind(1:2,11:12,101:102)
> idx <- cbind(c(1,2),c(2,3))
> row(idx)
     [,1] [,2]
[1,]    1    1
[2,]    2    2
> cbind(as.vector(row(idx)), as.vector(idx))
     [,1] [,2]
[1,]    1    1
[2,]    2    2
[3,]    1    2
[4,]    2    3
> dm[cbind(as.vector(row(idx)), as.vector(idx))]
[1]   1  12  11 102
> array(dm[cbind(as.vector(row(idx)), as.vector(idx))], dim=dim(idx))
     [,1] [,2]
[1,]    1   11
[2,]   12  102
>

At Tuesday 12:43 PM 10/19/2004, you wrote:
ah sorry, here's an example:
> dm = cbind(1:2,11:12,101:102)
> dm
     [,1] [,2] [,3]
[1,]    1   11  101
[2,]    2   12  102

> idx=cbind(c(1,2),c(2,3))
> idx
     [,1] [,2]
[1,]    1    2
[2,]    2    3

the result I want to get:
1   11
12 102

that is: each row of idx gives the column index in dm

diana

Sundar Dorai-Raj wrote:

[EMAIL PROTECTED] wrote:

Hi,

I have the following indexing problem, can you help me please ?

Given:
dm = a data.frame or a matrix dm,
idx = a 2 columns (or any number) matrix with the same number of rows as dm

I want get a subset of dm, for each row, the columns which
specified by idx.

thank you, diana
Diana,
From what I gather it appears as if you want to split dm by all the unique rows of idx? Is that right? If so, you can do the following:
x <- split(dm, do.call("paste", as.data.frame(idx))
This will split dm into a list with each element a subset of dm corresponding to a unique row in idx. The length of the x will be the number of unique rows in idx.
If this is not what you want, please provide an example and what you expect to see.
--sundar



______________________________________________ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

______________________________________________ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to