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: [email protected] [mailto:[email protected]] On Behalf Of Steve Bellan Sent: Friday, July 04, 2014 12:53 AM To: [email protected] 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]] ______________________________________________ [email protected]<mailto:[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. ________________________________ 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]]
______________________________________________ [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.

