[R] extracting submatrix from a bigger one

Rui Barradas ruipbarradas at sapo.pt
Mon Jun 24 19:33:18 CEST 2013


Hello,

I had forgotten the much simpler solutions. The following should do it.

split(dataset, dataset$ID)


Rui Barradas

Em 24-06-2013 18:13, Bert Gunter escreveu:
> First of all, is your data structure a matrix or a data frame? They
> are different!
>
> Assuming the latter, a shorter version of Rui's answer that avoids
> unique() and automatically takes care of names is:
>
> result <- by(dataset, dataset$ID,I)
>
> See ?by, ?tapply, and ?split
>
> -- Bert
>
> On Mon, Jun 24, 2013 at 9:24 AM, Rui Barradas <ruipbarradas at sapo.pt> wrote:
>> Hello,
>>
>> Try the following.
>>
>>
>> result <- lapply(unique(dataset$ID), function(uid) dataset[dataset$ID ==
>> uid, ])
>> names(result) <- unique(dataset$ID)
>>
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>> Em 24-06-2013 15:36, matteo escreveu:
>>>
>>> Hi guys,
>>> I'm a newby, so sorry for the easy question.
>>>
>>> I have a matrix (459x28) in which a large number of observations are
>>> repeated (same placed sampled in different times).
>>> One of the columns is refers to the ID of the place of sampling.
>>> What I would like is to extract subset matrix for every point of sampling.
>>>
>>> I can do it manually, e.g. x1<-data.frame(dataset[dataset$ID=="x1",])
>>> but is it possible to write a script and let do it to R?
>>> So i got n submatrix of the n ID found in the original columns.
>>>
>>> Cheers
>>>
>>> Matteo
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>
>



More information about the R-help mailing list