No, I’m sorry.  That’s a mistake. I should have written:

row.active <- matrix(rbinom(K*steps, 1, .7), nr = K, nc = steps) == 1

so that this is a logical indexing vector, not just pulling out the first 
element. To avoid yielding NaNs (though that doesn’t really break the 
question), I’ve also changed the code slightly below

Thanks for pointing this out!

Steve

****
set.seed(1)
K <- 10
v1 <- rnorm(K)
v2 <- rnorm(K)
v3 <- rnorm(K)
steps <- 5
row.active <- matrix(rbinom(K*steps, 1, .7), nr = K, nc = steps)==1 ## changed

cbind(v1,v2,v3)

for(ii in 1:steps) {
    v1[row.active[,ii]] <- 2*v1[row.active[,ii]]
    v2[row.active[,ii]] <- 3*v3[row.active[,ii]]*(v1[row.active[,ii]]) # changed
    v3[row.active[,ii]] <- (v2[row.active[,ii]] + v3[row.active[,ii]] + 3)^2
}

cbind(v1,v2,v3)


Id like to be able to do this more succinctly with something like the 
following. This would also allow me to store all the variables in a matrix, 
which seems better than having so many variables (I actually have about 50).

v1 <- rnorm(K)
v2 <- rnorm(K)
v3 <- rnorm(K)
vars <- cbind(v1,v2,v3)

for(ii in 1:steps) {
    vars[row.active[,ii]] <- within(vars[row.active[,ii]], {
        v1 <- 2*v1
        v2 <- 3*v3*v1 # changed
        v3 <- (v2+v3 + 3)^2
    })
}


On Jul 4, 2014, at 2:51 AM, PIKAL Petr <petr.pi...@precheza.cz> wrote:

> Hi
>  
> Are you 100% sure that you always want select only first item from v1,v2 and 
> v3 and change it in each step of cycle and keep only last value from your 
> cycle in first item in vectors v1-3?
>  
> Because this is what your cycle does.
>  
> Petr
>  
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
> Behalf Of Steve Bellan
> Sent: Friday, July 04, 2014 12:53 AM
> To: r-help@r-project.org
> Subject: [R] applying operations within() a matrix's environment
>  
> I am trying to optimize code for speed and readability for a fairly complex 
> function inside an MCMC sampler. As of now I have a bunch of variables inside 
> a function that get iteratively reset inside a loop. Something like this:
> 
> set.seed(1)
> K <- 10
> v1 <- rnorm(K)
> v2 <- rnorm(K)
> v3 <- rnorm(K)
> steps <- 5
> row.active <- matrix(rbinom(K*steps, 1, .7), nr = K, nc = steps)
> 
> cbind(v1,v2,v3)
> 
> for(ii in 1:steps) {
>     v1[row.active[,ii]] <- 2*v1[row.active[,ii]]
>     v2[row.active[,ii]] <- 3*v3[row.active[,ii]]^(v1[row.active[,ii]])
>     v3[row.active[,ii]] <- (v2[row.active[,ii]] + v3[row.active[,ii]] + 3)^2
> }
> 
> cbind(v1,v2,v3)
> 
> 
> Id like to be able to do this more succinctly with something like the 
> following. This would also allow me to store all the variables in a matrix, 
> which seems better than having so many variables (I actually have about 50).
> 
> v1 <- rnorm(K)
> v2 <- rnorm(K)
> v3 <- rnorm(K)
> vars <- cbind(v1,v2,v3)
> 
> for(ii in 1:steps) {
>     vars[row.active[,ii]] <- within(vars[row.active[,ii]], {
>         v1 <- 2*v1
>         v2 <- 3*v3^v1
>         v3 <- (v2+v3 + 3)^2
>     })
> }
> 
> But I get this error because with() & within() don�t work on objects that 
> aren�t environments such as matrices.
> 
> 
> > + . + Error in UseMethod("within") : 
>   no applicable method for 'within' applied to an object of class 
> "c('double', 'numeric�)"
> 
> 
> It�s important that I keep this as a matrix and not a data.frame or list 
> because I�m trying to optimize for speed.  Help is much appreciated. Thanks!
> 
> Steve
> 
> 
> Steve Bellan, PhD, MPH
> Post-doctoral Researcher
> Lauren Ancel Meyers Research Group
> Center for Computational Biology and Bioinformatics
> University of Texas at Austin
> http://www.bio.utexas.edu/research/meyers/steve_bellan/
> 
> 
>         [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help@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.
> 
> Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a 
> jsou určeny pouze jeho adresátům.
> Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě 
> neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho 
> kopie vymažte ze svého systému.
> Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento 
> email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
> Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou 
> modifikacemi či zpožděním přenosu e-mailu.
> 
> V případě, že je tento e-mail součástí obchodního jednání:
> - vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření 
> smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
> - a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně 
> přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky 
> ze strany příjemce s dodatkem či odchylkou.
> - trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve 
> výslovným dosažením shody na všech jejích náležitostech.
> - odesílatel tohoto emailu informuje, že není oprávněn uzavírat za 
> společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně 
> zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly 
> adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, 
> předloženy nebo jejich existence je adresátovi či osobě jím zastoupené 
> známá.
> 
> This e-mail and any documents attached to it may be confidential and are 
> intended only for its intended recipients.
> If you received this e-mail by mistake, please immediately inform its sender. 
> Delete the contents of this e-mail with all attachments and its copies from 
> your system.
> If you are not the intended recipient of this e-mail, you are not authorized 
> to use, disseminate, copy or disclose this e-mail in any manner.
> The sender of this e-mail shall not be liable for any possible damage caused 
> by modifications of the e-mail or by delay with transfer of the email.
> 
> In case that this e-mail forms part of business dealings:
> - the sender reserves the right to end negotiations about entering into a 
> contract in any time, for any reason, and without stating any reasoning.
> - if the e-mail contains an offer, the recipient is entitled to immediately 
> accept such offer; The sender of this e-mail (offer) excludes any acceptance 
> of the offer on the part of the recipient containing any amendment or 
> variation.
> - the sender insists on that the respective contract is concluded only upon 
> an express mutual agreement on all its aspects.
> - the sender of this e-mail informs that he/she is not authorized to enter 
> into any contracts on behalf of the company except for cases in which he/she 
> is expressly authorized to do so in writing, and such authorization or power 
> of attorney is submitted to the recipient or the person represented by the 
> recipient, or the existence of such authorization is known to the recipient 
> of the person represented by the recipient.


        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to