Re: [R] Avoiding loops using 'for' and pairwise comparison of columns
Here's a possible solution to avoid the loop k <- as.matrix(expand.grid(1:ncol(x),1:ncol(x))) a1 <- as.data.frame(matrix(sapply(1:nrow(k), function(n) agree(x[,k[n,]])$value), nrow=ncol(x))) colnames(a1) <- colnames(x) rownames(a1) <- colnames(x) > identical(a, a1) [1] TRUE Or if you want to avoid double calculation, a2 <- as.data.frame(matrix(0, nrow=ncol(x), ncol=ncol(x))) colnames(a2) <- colnames(x) rownames(a2) <- colnames(x) k <- t(combn(1:ncol(x), 2)) a2[lower.tri(a2)] <- sapply(1:nrow(k), function(n) agree(x[,k[n,]])$value) a2 <- a2+diag(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] Avoiding loops using 'for' and pairwise comparison of columns 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,c2,c3) I need to compare every column with each other and want to know the percentage of similar values for each column pair. To calculate the percentage of similar values I used the function 'agree' from the irr-package. I solved the problem with a loop that is very slow. library(irr) # required for the function 'agree' # empty data frame for the results a <- as.data.frame(matrix(data=NA, nrow=3, ncol=3)) colnames(a) <- colnames(x) rownames(a) <- colnames(x) # the loop to write the data for (j in 1:ncol(x)){ for (i in 1:ncol(x)){ a[i,j] <- agree(cbind(x[,j], x[,i]))$value } } I would be very pleased to receive your suggestions how to avoid the loop. Furthermore the resulting data frame could be displayed as a diagonal matrix without duplicates of each pairwise comparison, but I don't know how to solve this problem. Kind regards Thomas __ 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. __ 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.
[R] Avoiding loops using 'for' and pairwise comparison of columns
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,c2,c3) I need to compare every column with each other and want to know the percentage of similar values for each column pair. To calculate the percentage of similar values I used the function 'agree' from the irr-package. I solved the problem with a loop that is very slow. library(irr) # required for the function 'agree' # empty data frame for the results a <- as.data.frame(matrix(data=NA, nrow=3, ncol=3)) colnames(a) <- colnames(x) rownames(a) <- colnames(x) # the loop to write the data for (j in 1:ncol(x)){ for (i in 1:ncol(x)){ a[i,j] <- agree(cbind(x[,j], x[,i]))$value } } I would be very pleased to receive your suggestions how to avoid the loop. Furthermore the resulting data frame could be displayed as a diagonal matrix without duplicates of each pairwise comparison, but I don't know how to solve this problem. Kind regards Thomas __ 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.
Re: [R] Avoiding loops to detect number of coincidences
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 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 to now how many positions are identical between each row > in pru compared with ht. I have no idea what you are trying to do with the loops below, but if you are trying to count matches by row: > a reproducible example > dput(ht) c(1, 1, 1, 8, 1, 1, 6, 4, 1, 3, 1, 3, 3) > dput(pru) structure(list(V1 = c(4L, 1L, 1L, 6L), V2 = c(1L, 6L, 1L, 6L), V3 = c(1L, 1L, 1L, 6L), V4 = c(8L, 8L, 8L, 8L), V5 = c(1L, 1L, 1L, 1L), V6 = c(1L, 1L, 1L, 1L), V7 = c(6L, 6L, 6L, 6L ), V8 = c(4L, 4L, 4L, 4L), V9 = c(1L, 1L, 1L, 1L), V10 = c(3L, 3L, 3L, 3L), V11 = c(1L, 1L, 1L, 1L), V12 = c(3L, 3L, 3L, 3L), V13 = c(3L, 3L, 3L, 3L)), .Names = c("V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", "V12", "V13" ), class = "data.frame", row.names = c(NA, -4L)) > # count the positional matches by row > apply(pru, 1, function(x)sum(x == ht)) [1] 12 12 13 10 Sarah > n and m are the col and row of pru (m is the same number in pru and ht) > > I tried this with loops > > n<-nrow(pru) > m<-ncol(pru) > > dacc2<-mat.or.vec(n, m) > > for (g in 1:n){ > for (j in 1:m){ > if(pru[g,j]-ht[1,j]!=0) dacc2[g,j]=0 else {dacc2[g,j]=1} > } > } > > So when I have dacc2 I can filter this: > > dar2<-pru[colSums(dacc2)>2 & colSums(dacc2)<10,] > > There is some way to avoid loops? > > [[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. > -- 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://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Avoiding loops to detect number of coincidences
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 to now how many positions are identical between each row in pru compared with ht. n and m are the col and row of pru (m is the same number in pru and ht) I tried this with loops n<-nrow(pru) m<-ncol(pru) dacc2<-mat.or.vec(n, m) for (g in 1:n){ for (j in 1:m){ if(pru[g,j]-ht[1,j]!=0) dacc2[g,j]=0 else {dacc2[g,j]=1} } } So when I have dacc2 I can filter this: dar2<-pru[colSums(dacc2)>2 & colSums(dacc2)<10,] There is some way to avoid loops? [[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.
[R] Avoiding loops in creating a coinvestment matrix
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). The output should look like the "coinvestments" matrix produced with the code below. If possible I would like to avoid loops and optimize the code for speed because the real data is huge. If anybody has suggestions, I would be grateful. invest=c(20,50,40,30,10,20,20,30,40) vc=rep(c('A','B','C'),each=3) company=c('E','F','G','F','G','H','G','H','I') data=data.frame(vc,company,invest) data #data inv.mat=tapply(invest,list(vc,company),sum) inv.mat=replace(inv.mat,which(is.na(inv.mat)==T),0) inv.mat #investment matrix exist.mat=inv.mat>0 coinvestments<-matrix(0,nrow=length(unique(vc)),ncol=length(unique(vc))) for(i in unique(vc)){ for(j in unique(vc)){ i.is=which(unique(vc)==i) j.is=which(unique(vc)==j) i.invests=exist.mat[i,] j.invests=exist.mat[j,] which.i=which(i.invests==T) which.j=which(j.invests==T) i.invests.with.j=which.i[which.i%in%which.j] coinvestments[i.is,j.is]=sum(inv.mat[i.is,i.invests.with.j]) } } coinvestments system.time( for(i in unique(vc)){ for(j in unique(vc)){ i.is=which(unique(vc)==i) j.is=which(unique(vc)==j) i.invests=exist.mat[i,] j.invests=exist.mat[j,] which.i=which(i.invests==T) which.j=which(j.invests==T) i.invests.with.j=which.i[which.i%in%which.j] coinvestments[i.is,j.is]=sum(inv.mat[i.is,i.invests.with.j]) } } ) Thanks much, Daniel -- View this message in context: http://r.789695.n4.nabble.com/Avoiding-loops-in-creating-a-coinvestment-matrix-tp3424298p3424298.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] Avoiding Loops When Iterating Over Statement That Updates Its Input
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 you need memory management in each iteration rather than once) and your objects may be copied around all the time. Uwe Ligges 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 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<- numeric(nrow(d)) r.seq[1]<- 2 * (1 / d$Dt[1] - 1) for (i in 2:nrow(d)) { r.seq[i]<- uniroot(bdt.deviation, interval = c(0, 1), D.T = d$Dt[i], r.prior = r.seq[i-1])$root } Uwe Ligges Alan On Tue, May 25, 2010 at 11:43 PM, Alan Luewrote: 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) for (i in 2:nrow(d)) { rf<- uniroot(bdt.deviation, interval=c(0, 1), D.T=d$Dt[i], r.prior=r.seq) r.seq<- append(r.seq, rf$root) } The call to `uniroot()' both updates `r.seq' and reads it as input. We could save the output of each invocation of `uniroot()' and concatenate it later, but is there a better way to write this (i.e., to execute more quickly) while updating `r.seq' in each iteration? Alan __ 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.
Re: [R] Avoiding Loops When Iterating Over Statement That Updates Its Input
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 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 <- numeric(nrow(d)) > r.seq[1] <- 2 * (1 / d$Dt[1] - 1) > for (i in 2:nrow(d)) { > r.seq[i] <- uniroot(bdt.deviation, interval = c(0, 1), > D.T = d$Dt[i], r.prior = r.seq[i-1])$root > } > > Uwe Ligges > > > >> 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 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) >>> for (i in 2:nrow(d)) { >>> rf<- uniroot(bdt.deviation, interval=c(0, 1), D.T=d$Dt[i], >>> r.prior=r.seq) >>> r.seq<- append(r.seq, rf$root) >>> } >>> >>> The call to `uniroot()' both updates `r.seq' and reads it as input. >>> We could save the output of each invocation of `uniroot()' and >>> concatenate it later, but is there a better way to write this (i.e., >>> to execute more quickly) while updating `r.seq' in each iteration? >>> >>> Alan >>> >> >> >> > -- Alan Lue Master of Financial Engineering UCLA Anderson School of Management __ 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.
Re: [R] Avoiding Loops When Iterating Over Statement That Updates Its Input
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 <- numeric(nrow(d)) r.seq[1] <- 2 * (1 / d$Dt[1] - 1) for (i in 2:nrow(d)) { r.seq[i] <- uniroot(bdt.deviation, interval = c(0, 1), D.T = d$Dt[i], r.prior = r.seq[i-1])$root } Uwe Ligges 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 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) for (i in 2:nrow(d)) { rf<- uniroot(bdt.deviation, interval=c(0, 1), D.T=d$Dt[i], r.prior=r.seq) r.seq<- append(r.seq, rf$root) } The call to `uniroot()' both updates `r.seq' and reads it as input. We could save the output of each invocation of `uniroot()' and concatenate it later, but is there a better way to write this (i.e., to execute more quickly) while updating `r.seq' in each iteration? Alan __ 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.
Re: [R] Avoiding Loops When Iterating Over Statement That Updates Its Input
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 > functionor to otherwise avoid using a loopwhen iterating over a > statement that updates its input. > That's not necessarily true. Loops that accumulate memory by copying and recopying objects will slow things down, but apply functions execute loops internally at the C level of code. If you loop efficiently in R, it can be as fast or faster than a given apply family function. > For example, here's some such code: > > r.seq <- 2 * (1 / d$Dt[1] - 1) > for (i in 2:nrow(d)) { > rf <- uniroot(bdt.deviation, interval=c(0, 1), D.T=d$Dt[i], r.prior=r.seq) > r.seq <- append(r.seq, rf$root) > } > > The call to `uniroot()' both updates `r.seq' and reads it as input. > We could save the output of each invocation of `uniroot()' and > concatenate it later, but is there a better way to write this (i.e., > to execute more quickly) while updating `r.seq' in each iteration? > I would look into the Vectorize() function for this task. HTH, Dennis > > Alan > > __ > 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. > [[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.
Re: [R] Avoiding Loops When Iterating Over Statement That Updates Its Input
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 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) > for (i in 2:nrow(d)) { > rf <- uniroot(bdt.deviation, interval=c(0, 1), D.T=d$Dt[i], r.prior=r.seq) > r.seq <- append(r.seq, rf$root) > } > > The call to `uniroot()' both updates `r.seq' and reads it as input. > We could save the output of each invocation of `uniroot()' and > concatenate it later, but is there a better way to write this (i.e., > to execute more quickly) while updating `r.seq' in each iteration? > > Alan > -- Alan Lue Master of Financial Engineering UCLA Anderson School of Management __ 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.
[R] Avoiding Loops When Iterating Over Statement That Updates Its Input
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) for (i in 2:nrow(d)) { rf <- uniroot(bdt.deviation, interval=c(0, 1), D.T=d$Dt[i], r.prior=r.seq) r.seq <- append(r.seq, rf$root) } The call to `uniroot()' both updates `r.seq' and reads it as input. We could save the output of each invocation of `uniroot()' and concatenate it later, but is there a better way to write this (i.e., to execute more quickly) while updating `r.seq' in each iteration? Alan __ 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.
Re: [R] avoiding loops in equation
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, function(x) > pex(x,pga,std), simplify=FALSE) > do.call(rbind, a3) # > > On Sat, Oct 17, 2009 at 5:51 AM, > Julius Tesoro > wrote: > > 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 the above function for each element of > acc using: > > > > for (x in 1:length(acc)){ > > a1<-pex(acc[x],pga,std) > > if(x==1){a2<-a1 > > }else > {a2<-rbind(a2,a1) > > > > } > > > > I want to recode the following using sapply > > > > a2<-sapply(acc, function(x) pex(x,pga,std)) > > > > but what I get is a different result. > > > > Anyone knows how to rewrite the above function without > using loops? > > > > Thanks everyone. > > > > __ > > 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. > > > > __ 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.
[R] avoiding loops in equation
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 the above function for each element of acc using: for (x in 1:length(acc)){ a1<-pex(acc[x],pga,std) if(x==1){a2<-a1 }else {a2<-rbind(a2,a1) } I want to recode the following using sapply a2<-sapply(acc, function(x) pex(x,pga,std)) but what I get is a different result. Anyone knows how to rewrite the above function without using loops? Thanks everyone. __ 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.
Re: [R] Avoiding loops
Martin, Thanks for showing the timing tests. It is important to see how the time (and memory usage) grows with the size of the problem, where size may be the number of rows or length of the lag. Here is another function to toss in the hat. It uses no loops and does all the sum by diff'ing a cumsum, which loses some precision. I think the big speedup comes from the calculation of startPos. You can also use approx(method="const") or some zoo function to do this. The gSum function, which computes sums of overlapping subsequences of its input, could be changed to a call to lapply without a dramatic lose of speed and thus avoid the precision problems. It also supposes that dat$a is already sorted. f.wwd <- function(dat, max=5) { # filtering approach a <- dat$a minStart <- a - max i <- rep(c(FALSE, TRUE), each=length(a))[order(c(minStart, a))] startPos <- cumsum(i)[!i] + 1 endPos <- seq(along=a) gSum <- function(x) { cs <- cumsum(x) cs[endPos] - cs[startPos] + x[startPos] } dat$b <- gSum(dat$b) dat$c <- gSum(dat$c) dat } Bill Dunlap TIBCO Software Inc - Spotfire Division wdunlap tibco.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...@tajo.ucsd.edu > Subject: Re: [R] Avoiding loops > > 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 an R-level loop (and a lot of C loops!), but the > length of the loop is only as long as the maximum lag > > > f0 <- function(df0, max_lag) > { > max_lag <- min(nrow(df0), max_lag) > a <- df0[[1]] > ans <- df <- df0[,-1, drop=FALSE] > for (lag in seq_len(max_lag)) { > idx <- diff(a, lag) <= max_lag > pad <- logical(lag) > ans[c(pad, idx),] <- ans[c(pad, idx),] + df[c(idx, pad),] > } > cbind(a, ans) > } > > it makes the assumption that 'a' is sorted and unique, as in a time > series. This > > f1 <- function(df0, max_lag) > { > max_lag <- min(nrow(df0), max_lag) > a <- df0[[1]] > ans <- df0[,-1, drop=FALSE] > lag <- 1 > while(sum(idx <- diff(a, lag) <= max_lag) != 0) { > pad <- logical(lag) > ans[c(pad, idx),] <- ans[c(pad, idx),] + df[c(idx, pad),] > lag <- lag + 1 > } > cbind(a, ans) > } > > relaxes the assumption that 'a' is unique, I think, but I > haven't tested > carefully; it seems to perform about the same as f0. I think there's a > clever recursive solution in there, too. > > This is my implementation of Phil's solution > > phil0 <- function(df0, max_lag) > { > with(df0, { > g <- function(x) > apply(df0[a - x >= -max_lag & a - x <= 0, c('b','c')], > 2, sum) > data.frame(a, t(sapply(a, g))) > }) > } > > Here's my implementation of Chuck Berry's solution > > chuck0 <- function(df0, max_lag) > { > criterion <- > as.matrix(dist(df0$a)) <= max_lag & outer(df0$a,df0$a,">=") > criterion %*% as.matrix(df0[, c("b","c")]) > } > > Here's a data generator > > setup <- function(n, m) > ## n: number of rows > ## m: expected counts per sum > { > a0 <- sort(sample(seq_len(m * n), n)) > data.frame(a=a0, b=as.integer(runif(n, 1, 10)), >c=as.integer(runif(n, 1, 10))) > } > > and a comparison with > > df0 <- setup(10^3, 3) > max_lag <- 5 > > > system.time(f0res <- f0(df0, max_lag), gcFirst=TRUE) >user system elapsed > 0.016 0.000 0.016 > > system.time(phil0res <- phil0(df0, max_lag), gcFirst=TRUE) >user system elapsed > 0.960 0.000 0.962 > > system.time(chuck0res <- chuck0(df0, 5), gcFirst=TRUE) >user system elapsed > 0.252 0.000 0.254 > > > all.equal(f0res, phil0res) > [1] TRUE > > > all.equal(as.matrix(f0res[,2:3]), chuck0res, check.attributes=FALSE) > [1] TRUE > > The f0 solution seems to be usable up to about a million rows, > > > df0 <- setup(10^6, 3) > > s
Re: [R] Avoiding loops
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 an R-level loop (and a lot of C loops!), but the length of the loop is only as long as the maximum lag f0 <- function(df0, max_lag) { max_lag <- min(nrow(df0), max_lag) a <- df0[[1]] ans <- df <- df0[,-1, drop=FALSE] for (lag in seq_len(max_lag)) { idx <- diff(a, lag) <= max_lag pad <- logical(lag) ans[c(pad, idx),] <- ans[c(pad, idx),] + df[c(idx, pad),] } cbind(a, ans) } it makes the assumption that 'a' is sorted and unique, as in a time series. This f1 <- function(df0, max_lag) { max_lag <- min(nrow(df0), max_lag) a <- df0[[1]] ans <- df0[,-1, drop=FALSE] lag <- 1 while(sum(idx <- diff(a, lag) <= max_lag) != 0) { pad <- logical(lag) ans[c(pad, idx),] <- ans[c(pad, idx),] + df[c(idx, pad),] lag <- lag + 1 } cbind(a, ans) } relaxes the assumption that 'a' is unique, I think, but I haven't tested carefully; it seems to perform about the same as f0. I think there's a clever recursive solution in there, too. This is my implementation of Phil's solution phil0 <- function(df0, max_lag) { with(df0, { g <- function(x) apply(df0[a - x >= -max_lag & a - x <= 0, c('b','c')], 2, sum) data.frame(a, t(sapply(a, g))) }) } Here's my implementation of Chuck Berry's solution chuck0 <- function(df0, max_lag) { criterion <- as.matrix(dist(df0$a)) <= max_lag & outer(df0$a,df0$a,">=") criterion %*% as.matrix(df0[, c("b","c")]) } Here's a data generator setup <- function(n, m) ## n: number of rows ## m: expected counts per sum { a0 <- sort(sample(seq_len(m * n), n)) data.frame(a=a0, b=as.integer(runif(n, 1, 10)), c=as.integer(runif(n, 1, 10))) } and a comparison with df0 <- setup(10^3, 3) max_lag <- 5 > system.time(f0res <- f0(df0, max_lag), gcFirst=TRUE) user system elapsed 0.016 0.000 0.016 > system.time(phil0res <- phil0(df0, max_lag), gcFirst=TRUE) user system elapsed 0.960 0.000 0.962 > system.time(chuck0res <- chuck0(df0, 5), gcFirst=TRUE) user system elapsed 0.252 0.000 0.254 > all.equal(f0res, phil0res) [1] TRUE > all.equal(as.matrix(f0res[,2:3]), chuck0res, check.attributes=FALSE) [1] TRUE The f0 solution seems to be usable up to about a million rows, > df0 <- setup(10^6, 3) > system.time(f0res <- f0(df0, max_lag), gcFirst=TRUE) user system elapsed 2.680 0.004 2.700 Martin > > allie > > On 9/2/2009 3:13 AM, Phil Spector wrote: >> 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 >> Statistical Computing Facility >> Department of Statistics >> UC Berkeley >> spec...@stat.berkeley.edu >> >> >> >> On Tue, 1 Sep 2009, dolar wrote: >> >>> 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=5 >>> i'd like to sum up b and sum up c with the above rule >>> since 5, 4 and 1 are less than (within a distance of 5) or equal to >>> 5, then >>> we should get the following result: >>> >>> a b c >>> 5 13 14 >>> >>> the overall result should be >>> a b c >>> 1 5 2 >>> 4 11 11 >>> 5 13 14 >>> 8 11 14 >>> >>> how can i do this without a loop? >>> -- >>> View this message in context: >>> http://www.nabble.com/Avoiding-loops-tp25251376p25251376.html >>> Sent from the R help mailing list archive at Nabble.com. >>> >>> __ >>> 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. >>> >> __ >> 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. > > __ > 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-g
Re: [R] Avoiding loops
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 c 1 5 2 2 11 11 3 13 14 4 11 14 HTH, Chuck 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=5 i'd like to sum up b and sum up c with the above rule since 5, 4 and 1 are less than (within a distance of 5) or equal to 5, then we should get the following result: a b c 5 13 14 the overall result should be a b c 1 5 2 4 11 11 5 13 14 8 11 14 how can i do this without a loop? -- View this message in context: http://www.nabble.com/Avoiding-loops-tp25251376p25251376.html Sent from the R help mailing list archive at Nabble.com. __ 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. Charles C. Berry(858) 534-2098 Dept of Family/Preventive Medicine E mailto:cbe...@tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ 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.
Re: [R] Avoiding loops
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 Statistical Computing Facility Department of Statistics UC Berkeley spec...@stat.berkeley.edu On Wed, 2 Sep 2009, 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. allie On 9/2/2009 3:13 AM, Phil Spector wrote: 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 Statistical Computing Facility Department of Statistics UC Berkeley spec...@stat.berkeley.edu On Tue, 1 Sep 2009, dolar wrote: 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=5 i'd like to sum up b and sum up c with the above rule since 5, 4 and 1 are less than (within a distance of 5) or equal to 5, then we should get the following result: a b c 5 13 14 the overall result should be a b c 1 5 2 4 11 11 5 13 14 8 11 14 how can i do this without a loop? -- View this message in context: http://www.nabble.com/Avoiding-loops-tp25251376p25251376.html Sent from the R help mailing list archive at Nabble.com. __ 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. __ 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. __ 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. __ 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.
Re: [R] Avoiding loops
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 solution presented itself, but if not, for loops are fine with me. my $0.02 Stephen On Wed, Sep 2, 2009 at 9:25 AM, hadley wickham wrote: >> 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://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. > -- Stephen Sefick Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis __ 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.
Re: [R] Avoiding loops
> 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://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.
Re: [R] Avoiding loops
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 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 > Statistical Computing Facility > Department of Statistics > UC Berkeley > spec...@stat.berkeley.edu > > > > On Tue, 1 Sep 2009, dolar wrote: > >> >> 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=5 >> i'd like to sum up b and sum up c with the above rule >> since 5, 4 and 1 are less than (within a distance of 5) or equal to >> 5, then >> we should get the following result: >> >> a b c >> 5 13 14 >> >> the overall result should be >> a b c >> 1 5 2 >> 4 11 11 >> 5 13 14 >> 8 11 14 >> >> how can i do this without a loop? >> -- >> View this message in context: >> http://www.nabble.com/Avoiding-loops-tp25251376p25251376.html >> Sent from the R help mailing list archive at Nabble.com. >> >> __ >> 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. >> > > __ > 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. __ 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.
Re: [R] Avoiding loops
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 Statistical Computing Facility Department of Statistics UC Berkeley spec...@stat.berkeley.edu On Tue, 1 Sep 2009, dolar wrote: 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=5 i'd like to sum up b and sum up c with the above rule since 5, 4 and 1 are less than (within a distance of 5) or equal to 5, then we should get the following result: a b c 5 13 14 the overall result should be a b c 1 5 2 4 11 11 5 13 14 8 11 14 how can i do this without a loop? -- View this message in context: http://www.nabble.com/Avoiding-loops-tp25251376p25251376.html Sent from the R help mailing list archive at Nabble.com. __ 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. __ 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.
[R] Avoiding loops
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=5 i'd like to sum up b and sum up c with the above rule since 5, 4 and 1 are less than (within a distance of 5) or equal to 5, then we should get the following result: a b c 5 13 14 the overall result should be a b c 1 5 2 4 11 11 5 13 14 8 11 14 how can i do this without a loop? -- View this message in context: http://www.nabble.com/Avoiding-loops-tp25251376p25251376.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] Avoiding loops & apply -function
"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 = c(L,nlon,nlat) ) DM> data <- matrix(Y, nrow=nlon, ncol=nlat ) DM> G <- vector(length=L) DM> DM>for (l in 1:L) DM> { DM> G[l] <- function.F(data,E[l,,]) DM> } DM> DM> DM> - "E" is a 3-dimensional array filled with a given data X. DM> - "data" is a (nlon*nlat) matrix filled with given data Y. DM> - "G" is my output vector. DM> DM> I want to apply the function "function.F", but this function has an DM> argument that depends DM> on the index "l" ... do you mean something like that? sapply(1:L, function(idx, data, E) function.F(data, E[idx,,]), data, E) __ 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.
[R] Avoiding loops & apply -function
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(data,E[l,,]) } - "E" is a 3-dimensional array filled with a given data X. - "data" is a (nlon*nlat) matrix filled with given data Y. - "G" is my output vector. I want to apply the function "function.F", but this function has an argument that depends on the index "l" ... Thanks for your help! David __ 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.
Re: [R] avoiding loops
Ingmar Visser asks: > > Thanks for this. > I was afraid someone was going to say this ... > Does this mean the only way of getting this to run faster is by > moving to C code? Perhaps, but that's not all that difficult for this kind of operation, surely? Even portability across platforms should be pretty manageable. > The cases I'm thinking of applying this in have dimensions of A that > are much larger than > the example, eg n by n by T where n has a max of 10 or so but T could > be hundreds > or even thousands. What I would try first, then is a loop which makes maximum use of vectorisation. In this case it would be two loops, of course: d <- dim(A) for(i in 1:d[1]) for(j in 1:d[2]) A[i,j,] <- A[i,j,] * B But the following non-loop solution might be your best option: A <- aperm(aperm(A, c(3,1,2)) * as.vector(B), c(2,3,1)) If you can get your head around working with an A array where what is now the third dimension becomes the first, then you can eliminate the aperm()s and it becomes about as fast as C code (which it is, in effect). Permuting large arrays can be heavy on memory usage. Bill Venables. > Best, Ingmar > > On Mar 27, 2008, at 12:11 AM, <[EMAIL PROTECTED]> > <[EMAIL PROTECTED]> wrote: > > > If you have lots of memory there is an obvious strategy: > > > > d12 <- prod(dim(A)[1:2]) > > A <- A * array(rep(B, each = d12), dim = dim(A)) > > > > I don't really see much wrong with the obvious for() loop, though: > > > > for(b in 1:length(B)) A[,,b] <- A[,,b] * B[b] > > > > > > Bill Venables > > CSIRO Laboratories > > PO Box 120, Cleveland, 4163 > > AUSTRALIA > > Office Phone (email preferred): +61 7 3826 7251 > > Fax (if absolutely necessary): +61 7 3826 7304 > > Mobile: +61 4 8819 4402 > > Home Phone: +61 7 3286 7700 > > mailto:[EMAIL PROTECTED] > > http://www.cmis.csiro.au/bill.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 > > > > 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. > > > > Hence the result should have the same dimension as A, ie a series of > > 2 by 2 matrices. > > > > Short of a for loop over the the last index of A I have struggled > > with versions of apply but > > to no avail ... > > > > Any insights much appreciated, > > > > Best, Ingmar > > > > __ > > 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. > > > > > __ 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.
Re: [R] avoiding loops
Thanks for this. I was afraid someone was going to say this ... Does this mean the only way of getting this to run faster is by moving to C code? The cases I'm thinking of applying this in have dimensions of A that are much larger than the example, eg n by n by T where n has a max of 10 or so but T could be hundreds or even thousands. Best, Ingmar On Mar 27, 2008, at 12:11 AM, <[EMAIL PROTECTED]> <[EMAIL PROTECTED]> wrote: > If you have lots of memory there is an obvious strategy: > > d12 <- prod(dim(A)[1:2]) > A <- A * array(rep(B, each = d12), dim = dim(A)) > > I don't really see much wrong with the obvious for() loop, though: > > for(b in 1:length(B)) A[,,b] <- A[,,b] * B[b] > > > Bill Venables > CSIRO Laboratories > PO Box 120, Cleveland, 4163 > AUSTRALIA > Office Phone (email preferred): +61 7 3826 7251 > Fax (if absolutely necessary): +61 7 3826 7304 > Mobile: +61 4 8819 4402 > Home Phone: +61 7 3286 7700 > mailto:[EMAIL PROTECTED] > http://www.cmis.csiro.au/bill.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 > > 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. > > Hence the result should have the same dimension as A, ie a series of > 2 by 2 matrices. > > Short of a for loop over the the last index of A I have struggled > with versions of apply but > to no avail ... > > Any insights much appreciated, > > Best, Ingmar > > __ > 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. > > __ 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.
Re: [R] avoiding loops
If you have lots of memory there is an obvious strategy: d12 <- prod(dim(A)[1:2]) A <- A * array(rep(B, each = d12), dim = dim(A)) I don't really see much wrong with the obvious for() loop, though: for(b in 1:length(B)) A[,,b] <- A[,,b] * B[b] Bill Venables CSIRO Laboratories PO Box 120, Cleveland, 4163 AUSTRALIA Office Phone (email preferred): +61 7 3826 7251 Fax (if absolutely necessary): +61 7 3826 7304 Mobile: +61 4 8819 4402 Home Phone: +61 7 3286 7700 mailto:[EMAIL PROTECTED] http://www.cmis.csiro.au/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,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. Hence the result should have the same dimension as A, ie a series of 2 by 2 matrices. Short of a for loop over the the last index of A I have struggled with versions of apply but to no avail ... Any insights much appreciated, Best, Ingmar __ 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. __ 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.
[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,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. Hence the result should have the same dimension as A, ie a series of 2 by 2 matrices. Short of a for loop over the the last index of A I have struggled with versions of apply but to no avail ... Any insights much appreciated, Best, Ingmar __ 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.