Re: [R] Avoiding loops using 'for' and pairwise comparison of columns

2013-06-24 Thread Blaser Nello
100,ncol(x)) a2[upper.tri(a2)] <- t(a2)[upper.tri(a2)] > identical(a, a2) [1] TRUE Best, Nello -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Kulupp Sent: Montag, 24. Juni 2013 11:02 To: r-help@r-project.org Subject: [R] Avoidi

[R] Avoiding loops using 'for' and pairwise comparison of columns

2013-06-24 Thread Kulupp
Dear R-experts, I'd like to avoid the use of very slow 'for'-loops but I don't know how. My data look as follows (the original data has 1600 rows and 30 columns): # data example c1 <- c(1,1,1,0.25,0,1,1,1,0,1) c2 <- c(0,0,1,1,0,1,0,1,0.5,1) c3 <- c(0,1,1,1,0,0.75,1,1,0.5,0) x <- data.frame(c1,

Re: [R] Avoiding loops to detect number of coincidences

2011-07-12 Thread Sarah Goslee
Hi Trying, It would be helpful if you provided reproducible examples. It would also be polite to sign a name so that we have something by which to address you. On Tue, Jul 12, 2011 at 8:00 AM, Trying To learn again wrote: > Hi all, > > I have this information on a file ht.txt, imagine it is a da

[R] Avoiding loops to detect number of coincidences

2011-07-12 Thread Trying To learn again
Hi all, I have this information on a file ht.txt, imagine it is a data frame without labels: 1 1 1 8 1 1 6 4 1 3 1 3 3 And on other table called "pru.txt" I have sequences similar this 4 1 1 8 1 1 6 4 1 3 1 3 3 1 6 1 8 1 1 6 4 1 3 1 3 3 1 1 1 8 1 1 6 4 1 3 1 3 3 6 6 6 8 1 1 6 4 1 3 1 3 3 I want

[R] Avoiding loops in creating a coinvestment matrix

2011-04-03 Thread Daniel Malter
Hi, I am working on a dataset in which a number of venture capitalists invest in a number of firms. What I am creating is an asymmetric matrix M in which m(ij) is the volume (sum) of coinvestments of VC i with VC j (i.e., how much has VC i invested in companies that VC j also has investments in). T

Re: [R] Avoiding Loops When Iterating Over Statement That Updates Its Input

2010-05-30 Thread Uwe Ligges
On 30.05.2010 19:23, Alan Lue wrote: Is there a performance advantage to doing this, as opposed to growing the vector within the loop? I suppose R could have to dynamically reallocate memory at some point? Right, but that takes time since memory management is always expensive (and this way

Re: [R] Avoiding Loops When Iterating Over Statement That Updates Its Input

2010-05-30 Thread Alan Lue
Is there a performance advantage to doing this, as opposed to growing the vector within the loop? I suppose R could have to dynamically reallocate memory at some point? Alan 2010/5/30 Uwe Ligges : > > > On 26.05.2010 08:52, Alan Lue wrote: >> >> Come to think of it, we can't save the output of

Re: [R] Avoiding Loops When Iterating Over Statement That Updates Its Input

2010-05-30 Thread Uwe Ligges
On 26.05.2010 08:52, Alan Lue wrote: Come to think of it, we can't save the output of each invocation and concatenate it later, since we need the output as input for the next iteration. Yes, but you can do it a bit cleverer than before by initializing to the fill length as in: r.seq <- nu

Re: [R] Avoiding Loops When Iterating Over Statement That Updates Its Input

2010-05-26 Thread Dennis Murphy
Hi: On Tue, May 25, 2010 at 11:43 PM, Alan Lue wrote: > Since `for' loops are slow in R, and since `apply' functions are > faster, I was wondering whether there were a way to use an apply > function—or to otherwise avoid using a loop—when iterating over a > statement that updates its input. > T

Re: [R] Avoiding Loops When Iterating Over Statement That Updates Its Input

2010-05-25 Thread Alan Lue
Come to think of it, we can't save the output of each invocation and concatenate it later, since we need the output as input for the next iteration. Alan On Tue, May 25, 2010 at 11:43 PM, Alan Lue wrote: > Since `for' loops are slow in R, and since `apply' functions are > faster, I was wonderin

[R] Avoiding Loops When Iterating Over Statement That Updates Its Input

2010-05-25 Thread Alan Lue
Since `for' loops are slow in R, and since `apply' functions are faster, I was wondering whether there were a way to use an apply function—or to otherwise avoid using a loop—when iterating over a statement that updates its input. For example, here's some such code: r.seq <- 2 * (1 / d$Dt[1] - 1)

Re: [R] avoiding loops in equation

2009-10-17 Thread Julius Tesoro
thank you very much --- On Sat, 10/17/09, Kenn Konstabel wrote: > From: Kenn Konstabel > Subject: Re: [R] avoiding loops in equation Thank God for R-help mailing list. Thanks.. > To: "Julius Tesoro" > Date: Saturday, October 17, 2009, 1:51 PM > a3 <- sapply(acc,

[R] avoiding loops in equation

2009-10-16 Thread Julius Tesoro
To illustrate my problem, I have a complex code of several matrices and a vector. To simplify I only used two matrices and a vector as an example: pex<-function(acc, pga, std){ (acc-pga)/std } acc<-seq(.1,1,.1) pga<-matrix(rnorm(9,4,.1),3,3) std<-matrix(rnorm(9,4,.5),3,3) I tried calculating

Re: [R] Avoiding loops

2009-09-02 Thread William Dunlap
bco.com > -Original Message- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of Martin Morgan > Sent: Wednesday, September 02, 2009 9:17 AM > To: Alexander Shenkin > Cc: r-help@r-project.org; spec...@stat.berkeley.edu; > cbe

Re: [R] Avoiding loops

2009-09-02 Thread Martin Morgan
Alexander Shenkin wrote: > Though, from my limited understanding, the 'apply' family of functions > are actually just loops. Please correct me if I'm wrong. So, while > more readable (which is important), they're not necessarily more > efficient than explicit 'for' loops. Hi Allie -- This uses a

Re: [R] Avoiding loops

2009-09-02 Thread Charles C. Berry
On Tue, 1 Sep 2009, dolar wrote: Would like some tips on how to avoid loops as I know they are slow in R If I understand your criterion (and calling your data.frame 'dat'): criterion <- as.matrix(dist(dat$a)) <= 5 & outer(dat$a,dat$a,">=") criterion %*% as.matrix(dat[, c("b","c")]) b

Re: [R] Avoiding loops

2009-09-02 Thread Phil Spector
Another advantage of the apply family of functions is that they determine the size and type of their output in an efficient way, which is sometimes tricky when you write the loop yourself. - Phil Spector Statistica

Re: [R] Avoiding loops

2009-09-02 Thread stephen sefick
If you can do it- try a for loop and another solution to prove this to yourself. A for loop can get a little unwieldy for a novice like me to understand the code, but doable. The simpler the better, but they are not terribly slow. I have run into a couple of situations where a vectorized solutio

Re: [R] Avoiding loops

2009-09-02 Thread hadley wickham
> Would like some tips on how to avoid loops as I know they are slow in R They are not slow. They are slower than vectorised equivalents, but not slower than apply and friends. Hadley -- http://had.co.nz/ __ R-help@r-project.org mailing list https:/

Re: [R] Avoiding loops

2009-09-02 Thread Alexander Shenkin
Though, from my limited understanding, the 'apply' family of functions are actually just loops. Please correct me if I'm wrong. So, while more readable (which is important), they're not necessarily more efficient than explicit 'for' loops. allie On 9/2/2009 3:13 AM, Phil Spector wrote: > Here's

Re: [R] Avoiding loops

2009-09-02 Thread Phil Spector
Here's one way (assuming your data frame is named dat): with(dat, data.frame(a,t(sapply(a,function(x){ apply(dat[a - x >= -5 & a - x <= 0,c('b','c')],2,sum)} - Phil Spector St

[R] Avoiding loops

2009-09-01 Thread dolar
Would like some tips on how to avoid loops as I know they are slow in R i've got a data frame : a b c 1 5 2 4 6 9 5 2 3 8 3 2 What i'd like is to sum for each value of a, the sum of b and the sum of c where a equal to or less than (with a distance of 5) i.e. for row three we have a=

Re: [R] Avoiding loops & apply -function

2008-11-05 Thread Yohan Chalabi
"DM" == David Masson <[EMAIL PROTECTED]> on Wed, 05 Nov 2008 15:13:37 +0100 DM> I have a question concerning avoiding loops. DM> I know the function "apply" and I have used it several times, but I feel DM> blocked DM> with this situation : DM> DM> E <- array(X, dim =

[R] Avoiding loops & apply -function

2008-11-05 Thread David Masson
I have a question concerning avoiding loops. I know the function "apply" and I have used it several times, but I feel blocked with this situation : E <- array(X, dim = c(L,nlon,nlat) ) data <- matrix(Y, nrow=nlon, ncol=nlat ) G <- vector(length=L) for (l in 1:L) { G[l] <- function.F(

Re: [R] avoiding loops

2008-03-27 Thread Bill.Venables
ill.venables/ > > > > -Original Message- > > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] > > project.org] > > On Behalf Of Ingmar Visser > > Sent: Thursday, 27 March 2008 7:58 AM > > To: R-help@r-project.org > > Subject: [R] avoiding loops &

Re: [R] avoiding loops

2008-03-27 Thread Ingmar Visser
OTECTED] [mailto:[EMAIL PROTECTED] > project.org] > On Behalf Of Ingmar Visser > Sent: Thursday, 27 March 2008 7:58 AM > To: R-help@r-project.org > Subject: [R] avoiding loops > > Hi, > I need to compute an array from a matrix and an array: > > A <- array(1:20,

Re: [R] avoiding loops

2008-03-26 Thread Bill.Venables
-Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Ingmar Visser Sent: Thursday, 27 March 2008 7:58 AM To: R-help@r-project.org Subject: [R] avoiding loops Hi, I need to compute an array from a matrix and an array: A <- array(1:20,c(2,2,5)) B <- matrix(1:10,

[R] avoiding loops

2008-03-26 Thread Ingmar Visser
Hi, I need to compute an array from a matrix and an array: A <- array(1:20,c(2,2,5)) B <- matrix(1:10,5) And I would like the result to be an array consisting of the following: rbind(A[1,,1]*B[1,], A[2,,1]*B[1,]) rbind(A[1,,2]*B[2,], A[2,,2]*B[2,]) rbind(A[1,,3]*B[2,], A[2,,3]*B[2,]) etc. He