[R] indexing problem
diana
diana.chirac at free.fr
Tue Oct 19 20:43:37 CEST 2004
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:
>
>
> diana.chirac at free.fr 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
>
>
>
>
More information about the R-help
mailing list