Re: [R] vectorization of loops in R

2021-11-18 Thread PIKAL Petr
Hi above tapply and aggregate, split *apply could be used) sapply(with(df, split(z, y)), mean) Cheers Petr > -Original Message- > From: R-help On Behalf Of Luigi Marongiu > Sent: Wednesday, November 17, 2021 2:21 PM > To: r-help > Subject: [R] vectorization of loops in R > > Hello, >

Re: [R] vectorization of loops in R

2021-11-17 Thread Jan van der Laan
Have a look at the base functions tapply and aggregate. For example see: - https://cran.r-project.org/doc/manuals/r-release/R-intro.html#The-function-tapply_0028_0029-and-ragged-arrays , - https://online.stat.psu.edu/stat484/lesson/9/9.2, - or ?tapply and ?aggregate. Also your current code se

Re: [R] vectorization of loops in R

2021-11-17 Thread Kevin Thorpe
If I follow what you are trying to do, you want the mean of z for each value of y. tapply(df$z, df$y, mean) > On Nov 17, 2021, at 8:20 AM, Luigi Marongiu wrote: > > Hello, > I have a dataframe with 3 variables. I want to loop through it to get > the mean value of the variable `z`, as follows:

Re: [R] Vectorization in a random order

2016-11-10 Thread Jeff Newmiller
I think you answered your own question. For loops are not a boogeyman... poor memory management is. Algorithms that are sensitive to evaluation sequence are often not very re-usable, and certainly not parallelizable. If you have a specific algorithm in mind, there may be some advice we can give

Re: [R] Vectorization in a random order

2016-11-10 Thread Bert Gunter
You are mistaken. apply() is *not* vectorized. It is a disguised loop. For true vectorization at the C level, the answer must be no, as the whole point is to treat the argument as a whole object and hide the iterative details. However, as you indicated, you can always manually randomize the index

Re: [R] Vectorization in a random order

2016-11-10 Thread Richard M. Heiberger
nBuyMat <- data.frame(matrix(rnorm(28), 7, 4)) nBuyMat nBuy <- nrow(nBuyMat) sample(1:nBuy, nBuy, replace=FALSE) sample(1:nBuy) sample(nBuy) ?sample apply(nBuyMat[sample(1:nBuy,nBuy, replace=FALSE),], 1, function(x) sum(x)) apply(nBuyMat[sample(nBuy),], 1, function(x) sum(x)) The defaults for sa

Re: [R] vectorization of rolling function

2014-12-08 Thread Jeff Newmiller
Please don't post in HTML... you may not recognize it, but the receiving end does not necessarily (and in this case did not) look like the sending end, and the cleanup can impede answers you are hoping to get. In many cases, loops can be vectorized. However, near as I can tell this is an exam

Re: [R] vectorization of rolling function

2014-12-08 Thread Arnaud Duranel
Great, many thanks for your help Jeff. Apologies for the HTML format, I'll be more careful next time. Arnaud On 08/12/2014 08:25, Jeff Newmiller wrote: Please don't post in HTML... you may not recognize it, but the receiving end does not necessarily (and in this case did not) look like the send

Re: [R] vectorization

2014-01-29 Thread Bill
Oh wow, I guess I get it! Thank you. It is pretty tricky but I saw that it works very fast. On Wed, Jan 29, 2014 at 9:31 PM, Duncan Murdoch wrote: > On 14-01-29 6:41 AM, Bill wrote: > >> Hi. I saw this example and I cannot begin to figure out how it works. Can >> anyone give me an idea on this?

Re: [R] vectorization

2014-01-29 Thread Duncan Murdoch
On 14-01-29 6:41 AM, Bill wrote: Hi. I saw this example and I cannot begin to figure out how it works. Can anyone give me an idea on this? n = 9e6 df = data.frame(values = rnorm(n), ID = rep(LETTERS[1:3], each = n/3), stringsAsFactors = FALSE) head(df)

Re: [R] vectorization

2014-01-29 Thread PIKAL Petr
Hi everything is written in docs. However this example is a little tricky. Each df$ID matches name of item in translator_vector and [] selects this matched item. It is similar like x<-sample(1:3, 10,replace=T) translator_vector[x] Regards Petr > -Original Message- > From: r-help-boun

Re: [R] vectorization & modifying globals in functions

2012-12-28 Thread David Winsemius
On Dec 27, 2012, at 12:38 PM, Sam Steingold wrote: I have the following code: --8<---cut here---start->8--- d <- rep(10,10) for (i in 1:100) { a <- sample.int(length(d), size = 2) if (d[a[1]] >= 1) { d[a[1]] <- d[a[1]] - 1 d[a[2]] <- d[a[2]] + 1 } }

Re: [R] vectorization & modifying globals in functions

2012-12-28 Thread Greg Snow
In current versions of R the apply functions do not gain much (if any) in speed over a well written for loop (the for loops are much more efficient than they used to be). Using global variables could actually slow things down a little for what you are doing, if you use `<<-` then it has to search

Re: [R] vectorization & modifying globals in functions

2012-12-27 Thread Suzen, Mehmet
You can use environments. Have a look at this this discussion. http://stackoverflow.com/questions/7439110/what-is-the-difference-between-parent-frame-and-parent-env-in-r-how-do-they On 27 December 2012 21:38, Sam Steingold wrote: > I have the following code: > > --8<---cut here--

Re: [R] vectorization & modifying globals in functions

2012-12-27 Thread Neal H. Walfield
At Thu, 27 Dec 2012 15:38:08 -0500, Sam Steingold wrote: > so, > 1. is there a way for a function to modify a global variable? Use <<- instead of <-. > 2. how would you vectorize this loop? This is hard. Your function has a feedback loop: an iteration depends on the previous iteration's result.

Re: [R] vectorization condition counting

2012-08-10 Thread arun
: William Dunlap To: Guillaume2883 ; "r-help@r-project.org" Cc: Sent: Friday, August 10, 2012 8:02 PM Subject: Re: [R] vectorization condition counting Your sum(tag_id==tag_id[i])==1, meaning tag_id[i] is the only entry with its value, may be vectorized by the sneaky idiom   !(duplicate

Re: [R] vectorization condition counting

2012-08-10 Thread William Dunlap
Your sum(tag_id==tag_id[i])==1, meaning tag_id[i] is the only entry with its value, may be vectorized by the sneaky idiom !(duplicated(tag_id,fromLast=FALSE) | duplicated(tag_id,fromLast=TRUE) Hence f0() (with your code in a loop) and f1() are equivalent: f0 <- function (tags) { for (i in s

Re: [R] vectorization with subset?

2012-07-02 Thread David Winsemius
On Jul 2, 2012, at 5:16 PM, dlv04c wrote: The code is in the original post, but here it is again: No code here or in original posting to rhelp. You are under the delusion that Nabble is R-help. It is not. -- View this message in context: http://r.789695.n4.nabble.com/vectorization-with

Re: [R] vectorization with subset?

2012-07-02 Thread dlv04c
The code is in the original post, but here it is again: Thanks, Dan -- View this message in context: http://r.789695.n4.nabble.com/vectorization-with-subset-tp4635156p4635208.html Sent from the R help mailing list archive at Nabble.com. __ R-help@

Re: [R] vectorization with subset?

2012-07-02 Thread David Winsemius
On Jul 2, 2012, at 12:15 PM, dlv04c wrote: Hello, I have a data frame (68,000 rows) of scores (V4) for a series of [genomic] coordinates ranges (V2 to V3). I also have a data frame (1.2 million rows) of single [genomic] coordinates. For each genomic coordinate (in coord), I would l

Re: [R] Vectorization instead of loops problem

2011-12-04 Thread Bert Gunter
Inline below On Sun, Dec 4, 2011 at 10:29 AM, Costas Vorlow wrote: > Dear Bert, > > You are right (obviously). > > Apologies for any inconvenience caused.  I thought my problem was simplistic > with a very obvious answer which eluded me. > > As per your justified questions : > > 2: Answer is "all

Re: [R] Vectorization instead of loops problem

2011-12-04 Thread Costas Vorlow
Dear Bert, You are right (obviously). Apologies for any inconvenience caused. I thought my problem was simplistic with a very obvious answer which eluded me. As per your justified questions : 2: Answer is "all", hence: 3. would be include overlapping set (I guess) but this does not matter fo

Re: [R] Vectorization instead of loops problem

2011-12-04 Thread Bert Gunter
Costas: (and thanks for giving us your name) which(x == 1) gives you the indices where x is 1 (up to floating point equality -- you did not specify whether your x values are integers or calculated as floating point, and that certainly makes a difference). You can then use simple indexing to get t

Re: [R] Vectorization instead of loops problem

2011-12-04 Thread Gabor Grothendieck
On Sun, Dec 4, 2011 at 10:18 AM, Costas Vorlow wrote: > Hello, > > I am having problems vectorizing the following (i/o using a for/next/while > loop): > > I have 2 sequences such as: > > x, y > 1, 30 > 2, -40 > 0, 50 > 0, 25 > 1, -5 > 2, -10 > 1, 5 > 0, 40 > > etc etc > > The first sequence (x) ta

Re: [R] Vectorization instead of loops problem

2011-12-04 Thread Costas Vorlow
Thanks Uwe. What happens if these are zoo (or time series) sequences/dataframes? \ I think your solution would apply as well, no? Thanks again & best wishes, Costas 2011/12/4 Uwe Ligges > > > On 04.12.2011 16:18, Costas Vorlow wrote: > >> Hello, >> >> I am having problems vectorizing the follo

Re: [R] Vectorization instead of loops problem

2011-12-04 Thread Uwe Ligges
On 04.12.2011 16:18, Costas Vorlow wrote: Hello, I am having problems vectorizing the following (i/o using a for/next/while loop): I have 2 sequences such as: x, y 1, 30 2, -40 0, 50 0, 25 1, -5 2, -10 1, 5 0, 40 etc etc The first sequence (x) takes integer numbers only: 0, 1, 2 The sequen

Re: [R] Vectorization

2011-01-24 Thread Petr Savicky
On Sun, Jan 23, 2011 at 07:29:16PM -0800, eric wrote: > > Is there a way to vectorize this loop or a smarter way to do it ? > > y > [1] 0.003990746 -0.037664639 0.005397999 0.010415496 0.003500676 > [6] 0.001691775 0.008170774 0.011961998 -0.016879531 0.007284486 > [11] -0.015083581 -0.

Re: [R] Vectorization of three embedded loops

2009-01-14 Thread Thomas Terhoeven-Urselmans
Dear Carlos, thanks for your support. Patrick Burns gave me a hint, which is in the end very similar to your proposal. Now the script is roughly 25 times faster. Here is the code (I implemented as well an in size not increasing vector 'summ.dist<-rep(0,val.x.c.n)'): KEN.STO<-function(val.n

Re: [R] Vectorization of three embedded loops

2009-01-14 Thread Thomas Terhoeven-Urselmans
Dear Patrick, thanks for the very helpful response. I can calculate now 25 times faster. I use the 'k' from the outer-most loop only indirectly. It gives a maximal number of repetitions of the whole script until following command applies 'if(length(val.x.c)>=val.x.c.n)break'. The reason w

Re: [R] Vectorization of three embedded loops

2009-01-14 Thread Carlos J. Gil Bellosta
Hello, I believe that your bottleneck lies at this piece of code: sum<-c(); for(j in 1:length(val)){ sum[j]<-euc[rownames(start.b)[i],val[j]] } In order to speed up your code, there are two alternatives: 1) Try to reorder the euc matrix so that the sum vector corresponds to (part of) a

Re: [R] Vectorization of three embedded loops

2009-01-14 Thread Patrick Burns
You are definitely in Circle 2 of the R Inferno. Growing objects is suboptimal, although your objects are small so this probably isn't taking too much time. There is no need for the inner-most loop: sum.dist[i] <- min(euc[rownames(start.b)[i],val] ) Maybe I'm blind, but I don't see where 'k' c

Re: [R] vectorization instead of using loop

2008-10-09 Thread Richard . Cotton
Frank said: > > This piece of code works, but it is very slow. We were wondering if it's > at > > all possible to somehow vectorize this function. Any help would be > greatly > > appreciated. Richie said: > You can save a substantial time by calling as.matrix before the loop Patrick said: > On

Re: [R] vectorization instead of using loop

2008-10-09 Thread Richard . Cotton
> I've sent this question 2 days ago and got response from Sarah. Thanks for > that. But unfortunately, it did not really solve our problem. The main issue > is that we want to use our own (manipulated) covariance matrix in the > calculation of the mahalanobis distance. Does anyone know how to v

Re: [R] vectorization instead of using loop

2008-10-09 Thread Patrick Burns
One thing that would speed it up is if you inverted 'covmat' once and then used 'inverted=TRUE' in the call to 'mahalanobis'. Patrick Burns [EMAIL PROTECTED] +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") Frank Hedler wrote: Dear all, I'v

Re: [R] vectorization of a loop for mahalanobis distance calculation

2008-10-07 Thread Frank Hedler
Dear all,we just realized something. Sarah's distance function - indeed - calculates mahalanobis distance very well. However, it uses the observed variance-covariance matrix by default. What we actually need (sorry for not stating it clearly in to be able to specify which variance-covariance matrix

Re: [R] vectorization of a loop for mahalanobis distance calculation

2008-10-07 Thread Sarah Goslee
Hi Frank, If the way distance() calculates the Mahalanobis distance meets your needs other than the covariance specification, you can tweak that _very_ easily. If you use fix(distance) at the command line, you can edit the source. change the first line to: function (x, method = "euclidean", icov)

Re: [R] vectorization of a loop for mahalanobis distance calculation

2008-10-07 Thread Sarah Goslee
distance() from the ecodist package will calculate Mahalanobis distances. Sarah -- Sarah Goslee http://www.functionaldiversity.org __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http:

Re: [R] Vectorization of duration of the game in the gambler ruin's problem

2008-08-14 Thread Moshe Olshansky
Hi Jose, If you are only interested in the expected duration, the problem can be solved analytically - no simulation is needed. Let P be the probability to get total.capital (and then 1-P is the probability to loose all the money) when starting with initial.capital. This probability P is well k

Re: [R] Vectorization Problem

2008-03-22 Thread David Winsemius
"Sergey Goriatchev" <[EMAIL PROTECTED]> wrote in news:[EMAIL PROTECTED]: > I have the code for the bivariate Gaussian copula. It is written > with for-loops, it works, but I wonder if there is a way to > vectorize the function. > I don't see how outer() can be used in this case, but maybe one can

Re: [R] Vectorization/Speed Problem

2007-11-20 Thread Gabor Grothendieck
Let x be the input vector and cx be the cumulative running sum of it. Then seq_along(cx) - match(cx, cx) gives increasing sequences starting at 0 and for those after the leading zeros we start them at 1 by adding cummax(x). x <- c(0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0) # input cx <- cumsum(x)