[R] How do I use "tapply" in this case ?

David Winsemius dwinsemius at comcast.net
Fri Feb 5 04:39:10 CET 2010


On Feb 4, 2010, at 9:51 PM, Carrie Li wrote:

> Dear R-helpers ,
>
> I have a simple loop as follows, however, to be more efficient, I  
> would like
> to use any apply functions (tapply, I suppose)
> But how can I do this ? I am not very clear about this.
>
> # Z is a P * Q matrix
> # so for each row of Z, I would like to pull out only some of the  
> elements,
> and save as a separate matrix under a list
> # index is a vector with length smaller than Q, but the index could be
> different for each row of Z.

An example would help. Sounds like you need a list structure to hold  
the index and I don't see that you have constructed one.
>
> for (i in 1:P)
> {
>  tmp = matrix(Z[i, index], nrow=1)

Seems as though you would need to "index" in a verb sense (with "i")  
the index (in a noun sense)  structure. Might be better to call it  
"idx".
Perhaos

tmp = matrix(Z[i, index[[i]] ], nrow=1)  # the nrow=1 appears  
superfluous


>  tmpr[[i]]=tmp
> }
>

> any help is highly appreciated!!
>
> Thank you all
>
> Carrie
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list