[R] indexing problem
Sundar Dorai-Raj
sundar.dorai-raj at PDF.COM
Tue Oct 19 21:26:06 CEST 2004
diana 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
How about:
sapply(seq(nrow(dm)), function(i) dm[i, idx[i, ]])
--sundar
>
> 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
>>
>>
>>
>>
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
More information about the R-help
mailing list