Hi:
Here's another approach: use apply to do the sampling inline. Using Carrie's
original problem,
apply(Z, 1, function(x) x[sample(1:length(x), 3)])
[,1] [,2] [,3] [,4]
[1,] 0.6236041 -0.7554920 0.58903794 0.8390664
[2,] 0.8291094 0.4041808 0.07874168 1.2790384
[3,] -0.2517490 -0.4839661 -1.64292091 0.5100849
[results vary due to different sampled indices, but the principle's the
same.]
system time is competitive: (user time? I have a Windows box...enough said
:)
system.time(replicate(10000, apply(Z, 1, function(x) x[sample(1:length(x),
3)])))
user system elapsed
3.21 0.00 3.90
However...
> Z1 <- matrix(rnorm(2000000), nrow = 5)
> system.time(apply(Z1, 1, function(x) x[sample(1:length(x), 3)]))
user system elapsed
0.07 0.05 0.16
> Z1 <- matrix(rnorm(5000000), nrow = 20)
> system.time(apply(Z1, 1, function(x) x[sample(1:length(x), 10)]))
user system elapsed
0.32 0.06 0.53
> system.time(apply(Z1, 1, function(x) x[sample(1:length(x), 5)]))
user system elapsed
0.42 0.05 0.55
> system.time(apply(Z1, 1, function(x) x[sample(1:length(x), 3)]))
user system elapsed
0.52 0.02 0.75
so it seems to operate pretty quickly in large matrices.
Aesthetically, I like Bert's approach the best. Matrix indexing is cool :)
FWIW,
Dennis
On Thu, Feb 4, 2010 at 8:53 PM, Carrie Li <[email protected]> wrote:
> Thank you, David
> Here is an example :
>
>
> Z=matrix(rnorm(20), nrow=4)
> index=replicate(4, sample(1:5, 3))
> P=4
> tmpr=list()
> for (i in 1:P)
> {
> tmp = Z[i,index[,i]]
> tmpr[[i]]=tmp
> }
>
>
> So, I am trying to pull out the values in Z for each row, but now I only
> want to pull out the values indexed by the matrix "index"
> And, since I have large P, so I would like to avoid loop.
> Any quick way to do this ?
>
> Thanks again!!
>
> Carrie
>
>
>
>
>
>
> On Thu, Feb 4, 2010 at 10:39 PM, David Winsemius <[email protected]
> >wrote:
>
> >
> > 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]]
> >>
> >> ______________________________________________
> >> [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
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
> > David Winsemius, MD
> > Heritage Laboratories
> > West Hartford, CT
> >
> >
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.