Re: [R] avoiding a loop: cumsum-like
Hi I have not seen any answer yet so I wil try (partly). I believe that the loop can be vectorised but I am a little bit lost in your fors and ifs. I found that first part of res is same as cumsum(tab$x.jour) until about 81st value. However I did not decipher how to compute the remaining part. I tried to add cumsum(tab$posit.lat) (after changing NA to 0) what is not correct. Probably some combination of logical operation and summing can do what you want. I thought that something like ((cumsum(tab$posit.lat)*0.8)*(cumsum(tab$x.jour)30)+cumsum(tab$x.jour )) can do it but the result is defferent from your computation. Not much of help, but maybe you can do better with above suggestion. Petr On 2 Nov 2006 at 11:15, GOUACHE David wrote: Date sent: Thu, 2 Nov 2006 11:15:49 +0100 From: GOUACHE David [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Subject:[R] avoiding a loop: cumsum-like Hello Rhelpers, I need to run the following loop over a large number of data-sets, and was wondering if it could somehow be vectorized. It's more or less a cumulative sum, but slightly more complex. Here's the code, and an example dataset (called tab in my code) follows. Thanks in advance for any suggestions! res-0 for (i in min(tab$Date):max(tab$Date)) { if (is.na(tab$posit.lat[tab$Date==i])==T) { res-c(res,res[length(res)]+tab$x.jour[tab$Date==i]) } else { if (res[tab$posit.lat[tab$Date==i]+1]30) { res-c(res,res[length(res)]+tab$x.jour[tab$Date==i]) } else { res-c(res,res[length(res)]+tab$x.jour[tab$Date==i]+0.8*res[tab$pos it.lat[tab$Date==i]+1]) } } } res[-1] Date x.jour posit.lat 35804 0 NA 35805 0 NA 35806 0 NA 35807 0 NA 35808 0 NA 35809 2.97338883 NA 35810 2.796389915 NA 35811 0 NA 35812 0 NA 35813 1.000711886 NA 35814 0.894422571 NA 35815 0 NA 35816 0 NA 35817 0 NA 35818 0 NA 35819 0 NA 35820 0 NA 35821 0 NA 35822 0 NA 35823 0 NA 35824 0 NA 35825 0 NA 35826 0 NA 35827 0 NA 35828 0 NA 35829 0 NA 35830 0 NA 35831 0 NA 35832 0 NA 35833 0 NA 35834 0 NA 35835 0 NA 35836 0 NA 35837 0 NA 35838 0 NA 35839 0 NA 35840 2.47237455 NA 35841 0 2 35842 0 3 35843 0 4 35844 0 5 35845 0 6 35846 0 7 35847 4.842160488 8 35848 2.432125036 9 35849 0 10 35850 0 12 35851 0 14 35852 0 16 35853 3.739683882 18 35854 1.980214421 20 35855 0 22 35856 0 24 35857 5.953444078 27 35858 6.455722475 29 35859 0 31 35860 3.798690334 32 35861 6.222993364 34 35862 3.746243098 35 35863 0 35 35864 0 36 35865 0 37 35866 0 38 35867 0 38 35868 0 39 35869 0 40 35870 0 41 35871 0 42 35872 0 43 35873 0 44 35874 0 45 35875 0 46 35876 0 47 35877 1.951774892 48 35878 0 49 35879 0 50 35880 1.702837643 50 35881 0 52 35882 0 53 35883 0 54 35884 0 55 35885 5.953444078 57 35886 0 58 35887 5.737515358 59 35888 0 61 35889 6.215941227 63 35890 4.731576675 64 35891 0 66 35892 2.255448314 66 35893 3.782283008 67 35894 3.244474546 68 35895 1.808553193 69 35896 2.622680002 70 35897 0 71 35898 0 72 35899 0 72 35900 1.7084177 73 35901 1.28455982 74 35902 2.320013736 76 35903 0 77 35904 0 78 35905 0 79 35906 0 79 35907 0 80 35908 6.716812458 81 35909 0 82 35910 6.796571531 84 35911 5.573668337 85 35912 5.42513958 86 35913 3.774513877 86 35914 0 87 35915 0 89 35916 0 90 35917 4.208252725 91 35918 0 92 35919 0 93 35920 0 95 35921 5.70023661 97 35922 0 98 35923 0 100 35924 0 102 35925 0 103 35926 0 104 David Gouache Arvalis - Institut du Végétal Station de La Miničre 78280 Guyancourt Tel: 01.30.12.96.22 / Port: 06.86.08.94.32 __ R-help@stat.math.ethz.ch 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. Petr Pikal [EMAIL PROTECTED] __ R-help@stat.math.ethz.ch 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 a loop: cumsum-like
Hello Rhelpers, I need to run the following loop over a large number of data-sets, and was wondering if it could somehow be vectorized. It's more or less a cumulative sum, but slightly more complex. Here's the code, and an example dataset (called tab in my code) follows. Thanks in advance for any suggestions! res-0 for (i in min(tab$Date):max(tab$Date)) { if (is.na(tab$posit.lat[tab$Date==i])==T) { res-c(res,res[length(res)]+tab$x.jour[tab$Date==i]) } else { if (res[tab$posit.lat[tab$Date==i]+1]30) { res-c(res,res[length(res)]+tab$x.jour[tab$Date==i]) } else { res-c(res,res[length(res)]+tab$x.jour[tab$Date==i]+0.8*res[tab$posit.lat[tab$Date==i]+1]) } } } res[-1] Datex.jour posit.lat 35804 0 NA 35805 0 NA 35806 0 NA 35807 0 NA 35808 0 NA 35809 2.97338883 NA 35810 2.796389915 NA 35811 0 NA 35812 0 NA 35813 1.000711886 NA 35814 0.894422571 NA 35815 0 NA 35816 0 NA 35817 0 NA 35818 0 NA 35819 0 NA 35820 0 NA 35821 0 NA 35822 0 NA 35823 0 NA 35824 0 NA 35825 0 NA 35826 0 NA 35827 0 NA 35828 0 NA 35829 0 NA 35830 0 NA 35831 0 NA 35832 0 NA 35833 0 NA 35834 0 NA 35835 0 NA 35836 0 NA 35837 0 NA 35838 0 NA 35839 0 NA 35840 2.47237455 NA 35841 0 2 35842 0 3 35843 0 4 35844 0 5 35845 0 6 35846 0 7 35847 4.842160488 8 35848 2.432125036 9 35849 0 10 35850 0 12 35851 0 14 35852 0 16 35853 3.739683882 18 35854 1.980214421 20 35855 0 22 35856 0 24 35857 5.953444078 27 35858 6.455722475 29 35859 0 31 35860 3.798690334 32 35861 6.222993364 34 35862 3.746243098 35 35863 0 35 35864 0 36 35865 0 37 35866 0 38 35867 0 38 35868 0 39 35869 0 40 35870 0 41 35871 0 42 35872 0 43 35873 0 44 35874 0 45 35875 0 46 35876 0 47 35877 1.951774892 48 35878 0 49 35879 0 50 35880 1.702837643 50 35881 0 52 35882 0 53 35883 0 54 35884 0 55 35885 5.953444078 57 35886 0 58 35887 5.737515358 59 35888 0 61 35889 6.215941227 63 35890 4.731576675 64 35891 0 66 35892 2.255448314 66 35893 3.782283008 67 35894 3.244474546 68 35895 1.808553193 69 35896 2.622680002 70 35897 0 71 35898 0 72 35899 0 72 35900 1.7084177 73 35901 1.28455982 74 35902 2.320013736 76 35903 0 77 35904 0 78 35905 0 79 35906 0 79 35907 0 80 35908 6.716812458 81 35909 0 82 35910 6.796571531 84 35911 5.573668337 85 35912 5.42513958 86 35913 3.774513877 86 35914 0 87 35915 0 89 35916 0 90 35917 4.208252725 91 35918 0 92 35919 0 93 35920 0 95 35921 5.70023661 97 35922 0 98 35923 0 100 35924 0 102 35925 0 103 35926 0 104 David Gouache Arvalis - Institut du Végétal Station de La Minière 78280 Guyancourt Tel: 01.30.12.96.22 / Port: 06.86.08.94.32 __ R-help@stat.math.ethz.ch 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 'for' loop
Hi R users I have a 1000 * 11 matrix (or data.frame). The first 10 columns consist of 0 or 1 (i.e., binary data) and the last column consists of real values. I would like to run a logistic regression with the first binary column as a dependent variable, and the second binary column and the last real value column as independent variables. After this, I save 'Z' value for the binary independent variable. Then, with the first binary column as a dependent variable, and the third binary column and the last real value column as independent variables. After this, I saved 'Z' value for the binary independent variable, keeping doing this ... Here is my simple R codes logistic.z.value -c() for (i in 1:9) { for (j in (i+1):10) { model - glm(X[,i] ~ X[,11] + X[,j], family=binomial(logit)) logistic.z.value - append(logistic.z.value,coef(summary(model))[3,3]) } } I would like to avoid using 'for' loop command. Is there any way for me to use some fast codes instead of using 'for' loop? Many thanks Taka, _ Stay in touch with old friends and meet new ones with Windows Live Spaces __ R-help@stat.math.ethz.ch 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 a loop
Mark, This is hardly a one-liner. It will only help you if you call it with large objects. If you have 0 in your data it needs even more extending. Here it is: myconstruct - function(aa) { aa - c(1,1,aa,1) bb - cumprod(aa) taa - aa1 difftaa - diff(taa) starts - which(c(FALSE,difftaa==1)) ends - which(c(FALSE,difftaa==-1)) correction - rep(0,length(aa)) correction[starts] - 1/bb[starts-2] correction[ends] - -1/bb[starts-2] correction - cumsum(correction) correction[correction==0] - 1 correction bbcor - bb*correction bbcor[!taa] - aa[!taa] bbcor[c(-1,-2,-length(bbcor))] } aa - runif(1,min=.5,max=1.5) system.time(target - constructLt(aa)) system.time(found - myconstruct(aa)) identical(target,found) max(abs(target-found)) system.time(target - constructLt(aa)) [1] 0.059 0.001 0.059 0.000 0.000 system.time(found - myconstruct(aa)) [1] 0.013 0.000 0.014 0.000 0.000 identical(target,found) [1] FALSE max(abs(target-found)) [1] 4.440892e-16 Appearently the outcome is different is some low decimal, due to other calculation method. Kees On Tuesday 24 October 2006 20:36, Leeds, Mark (IED) wrote: I think I asked a similar question 3 years ago to the Splus list and I think the answer was no or noone answered so noone should spend more than 5 minutes on this because it could definitely be a waste of time. My question is whether the function below can be rewritten without a for loop. apply is fine if it can be done that way but i doubt it. I call it a lot and would prefer to not loop. #--- -- constructLt-function(invector) { outvector-invector for ( i in 2:length(invector) ) { if ( invector[i] 1 ) { outvector[i]-invector[i]*outvector[i-1] } } return(outvector) } #--- - This is not an offer (or solicitation of an offer) to buy/sell the securities/instruments mentioned or an official confirmation. Morgan Stanley may deal as principal in or own or act as market maker for securities/instruments mentioned or may advise the issuers. This is not research and is not from MS Research but it may refer to a research analyst/research report. Unless indicated, these views are the author's and may differ from those of Morgan Stanley research or others in the Firm. We do not represent this is accurate or complete and we may not update this. Past performance is not indicative of future returns. For additional information, research reports and important disclosures, contact me or see https://secure.ms.com/servlet/cls. You should not use e-mail to request, authorize or effect the purchase or sale of any security or instrument, to send transfer instructions, or to effect any other transactions. We cannot guarantee that any such requests received via ! e-mail will be processed in a timely manner. This communication is solely for the addressee(s) and may contain confidential information. We do not waive confidentiality by mistransmission. Contact me if you do not wish to receive these communications. In the UK, this communication is directed in the UK to those persons who are market counterparties or intermediate customers (as defined in the UK Financial Services Authority's rules). [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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 a loop
On Wednesday 25 October 2006 07:36, Leeds, Mark (IED) wrote: I think I asked a similar question 3 years ago to the Splus list and I think the answer was no or noone answered so noone should spend more than 5 minutes on this because it could definitely be a waste of time. My question is whether the function below can be rewritten without a for loop. apply is fine if it can be done that way but i doubt it. I call it a lot and would prefer to not loop. #--- -- constructLt-function(invector) { outvector-invector for ( i in 2:length(invector) ) { if ( invector[i] 1 ) { outvector[i]-invector[i]*outvector[i-1] } } return(outvector) } Depending on the nature of your data, there is a faster way. It still involves looping, but not over the entire vector. Try the following: constructLt - function(invector) { outvector - invector cs - cumsum(rle(invector 1)$lengths) if (invector[1] 1) cs - c(1, cs) for (i in 0:(length(cs)%/%2 - 1)){ starti - cs[2*i + 1] stopi - cs[starti + 1] outvector[starti:stopi] - cumprod(invector[starti:stopi]) } return(outvector) } It is in the order of 3 times as fast for random vectors of considerable length ( 1000). For random vectors of length 50 it is about the same speed as the full looping algorithm. However if the data is such that there are longer runs (than N(1, 1)), then you might expect a better speedup. HTH Ray Brownrigg __ R-help@stat.math.ethz.ch 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 a loop
I think I asked a similar question 3 years ago to the Splus list and I think the answer was no or noone answered so noone should spend more than 5 minutes on this because it could definitely be a waste of time. My question is whether the function below can be rewritten without a for loop. apply is fine if it can be done that way but i doubt it. I call it a lot and would prefer to not loop. #--- -- constructLt-function(invector) { outvector-invector for ( i in 2:length(invector) ) { if ( invector[i] 1 ) { outvector[i]-invector[i]*outvector[i-1] } } return(outvector) } #--- - This is not an offer (or solicitation of an offer) to buy/sell the securities/instruments mentioned or an official confirmation. Morgan Stanley may deal as principal in or own or act as market maker for securities/instruments mentioned or may advise the issuers. This is not research and is not from MS Research but it may refer to a research analyst/research report. Unless indicated, these views are the author's and may differ from those of Morgan Stanley research or others in the Firm. We do not represent this is accurate or complete and we may not update this. Past performance is not indicative of future returns. For additional information, research reports and important disclosures, contact me or see https://secure.ms.com/servlet/cls. You should not use e-mail to request, authorize or effect the purchase or sale of any security or instrument, to send transfer instructions, or to effect any other transactions. We cannot guarantee that any such requests received via ! e-mail will be processed in a timely manner. This communication is solely for the addressee(s) and may contain confidential information. We do not waive confidentiality by mistransmission. Contact me if you do not wish to receive these communications. In the UK, this communication is directed in the UK to those persons who are market counterparties or intermediate customers (as defined in the UK Financial Services Authority's rules). [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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 a loop
On Tue, 2006-10-24 at 14:36 -0400, Leeds, Mark (IED) wrote: I think I asked a similar question 3 years ago to the Splus list and I think the answer was no or noone answered so noone should spend more than 5 minutes on this because it could definitely be a waste of time. My question is whether the function below can be rewritten without a for loop. apply is fine if it can be done that way but i doubt it. I call it a lot and would prefer to not loop. #--- -- constructLt-function(invector) { outvector-invector for ( i in 2:length(invector) ) { if ( invector[i] 1 ) { outvector[i]-invector[i]*outvector[i-1] } } return(outvector) } You sure can vectorize this. Try this below... I haven't tested, but it should be close to your solution. There's also a - invector[-1] outvector - invector wh - which(a1)+1 outvector[wh] - a[wh] * invector[-length(invector)][wh-1] outvector HTH, Jerome -- Jerome Asselin, M.Sc., Agent de recherche, RHCE CHUM -- Centre de recherche 3875 rue St-Urbain, 3e etage // Montreal QC H2W 1V1 Tel.: 514-890-8000 Poste 15914; Fax: 514-412-7106 __ R-help@stat.math.ethz.ch 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 a loop
On 10/24/2006 2:53 PM, Jerome Asselin wrote: On Tue, 2006-10-24 at 14:36 -0400, Leeds, Mark (IED) wrote: I think I asked a similar question 3 years ago to the Splus list and I think the answer was no or noone answered so noone should spend more than 5 minutes on this because it could definitely be a waste of time. My question is whether the function below can be rewritten without a for loop. apply is fine if it can be done that way but i doubt it. I call it a lot and would prefer to not loop. #--- -- constructLt-function(invector) { outvector-invector for ( i in 2:length(invector) ) { if ( invector[i] 1 ) { outvector[i]-invector[i]*outvector[i-1] } } return(outvector) } You sure can vectorize this. Try this below... I haven't tested, but it should be close to your solution. There's also a - invector[-1] outvector - invector wh - which(a1)+1 outvector[wh] - a[wh] * invector[-length(invector)][wh-1] outvector I think this misses the fact that if a sequence of invector values are less than 1, then the multiplication accumulates: e.g. with invector equal to rep(0.5, 5) the output should be constructLt(rep(0.5,5)) [1] 0.5 0.25000 0.12500 0.06250 0.03125 whereas your function gives asselin(rep(0.5,5)) [1] 0.50 0.25 0.25 0.25 NA A function like filter() might be able to do this, but in general R doesn't give a way to write recursive formulae in a simple vector form. In this particular case, I'd just use the for loop; if it turned out to be too slow, I'd rewrite it in C, in a nearly literal translation. Duncan Murdoch __ R-help@stat.math.ethz.ch 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 a loop
Try this (essentially the trick is to shift the invector to get the y[i-i] effect): constructLt-function(invector, a=1) { invector[invectora] - c(0,invector)[invectora] * invector[invectora] invector } aa - c(1,1,0.5,2,3,0.4,4,5) aa [1] 1.0 1.0 0.5 2.0 3.0 0.4 4.0 5.0 constructLt(aa) [1] 1.0 1.0 0.5 2.0 3.0 1.2 4.0 5.0 -Christos -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Leeds, Mark (IED) Sent: Tuesday, October 24, 2006 2:36 PM To: R-help@stat.math.ethz.ch Subject: [R] avoiding a loop I think I asked a similar question 3 years ago to the Splus list and I think the answer was no or noone answered so noone should spend more than 5 minutes on this because it could definitely be a waste of time. My question is whether the function below can be rewritten without a for loop. apply is fine if it can be done that way but i doubt it. I call it a lot and would prefer to not loop. #--- -- constructLt-function(invector) { outvector-invector for ( i in 2:length(invector) ) { if ( invector[i] 1 ) { outvector[i]-invector[i]*outvector[i-1] } } return(outvector) } #--- - This is not an offer (or solicitation of an offer) to buy/sell the securities/instruments mentioned or an official confirmation. Morgan Stanley may deal as principal in or own or act as market maker for securities/instruments mentioned or may advise the issuers. This is not research and is not from MS Research but it may refer to a research analyst/research report. Unless indicated, these views are the author's and may differ from those of Morgan Stanley research or others in the Firm. We do not represent this is accurate or complete and we may not update this. Past performance is not indicative of future returns. For additional information, research reports and important disclosures, contact me or see https://secure.ms.com/servlet/cls. You should not use e-mail to request, authorize or effect the purchase or sale of any security or instrument, to send transfer instructions, or to effect any other transactions. We cannot guarantee that any such requests received via ! e-mail will be processed in a timely manner. This communication is solely for the addressee(s) and may contain confidential information. We do not waive confidentiality by mistransmission. Contact me if you do not wish to receive these communications. In the UK, this communication is directed in the UK to those persons who are market counterparties or intermediate customers (as defined in the UK Financial Services Authority's rules). [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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 a loop
I appreciate your effort but I'm fairly certain that won't work. The loop I have written is pseudo recursive in the sense that outvector depends on its previous value and this previous value may have changed because it depnds on the previous value of itself. The fact that there is no outvector on the right side of your last line of code tells me that it's probably not what I want. I'll Try it though. It can't hurt. I do appreciate the help. -Original Message- From: Jerome Asselin [mailto:[EMAIL PROTECTED] Sent: Tuesday, October 24, 2006 2:54 PM To: Leeds, Mark (IED) Cc: R-help@stat.math.ethz.ch Subject: Re: [R] avoiding a loop On Tue, 2006-10-24 at 14:36 -0400, Leeds, Mark (IED) wrote: I think I asked a similar question 3 years ago to the Splus list and I think the answer was no or noone answered so noone should spend more than 5 minutes on this because it could definitely be a waste of time. My question is whether the function below can be rewritten without a for loop. apply is fine if it can be done that way but i doubt it. I call it a lot and would prefer to not loop. #- -- -- constructLt-function(invector) { outvector-invector for ( i in 2:length(invector) ) { if ( invector[i] 1 ) { outvector[i]-invector[i]*outvector[i-1] } } return(outvector) } You sure can vectorize this. Try this below... I haven't tested, but it should be close to your solution. There's also a - invector[-1] outvector - invector wh - which(a1)+1 outvector[wh] - a[wh] * invector[-length(invector)][wh-1] outvector HTH, Jerome -- Jerome Asselin, M.Sc., Agent de recherche, RHCE CHUM -- Centre de recherche 3875 rue St-Urbain, 3e etage // Montreal QC H2W 1V1 Tel.: 514-890-8000 Poste 15914; Fax: 514-412-7106 This is not an offer (or solicitation of an offer) to buy/se...{{dropped}} __ R-help@stat.math.ethz.ch 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 a loop
thanks duncan. I think you are right. There are very smart people on this list but there were very smart people on The splus list 3 years ago so that's why I presented my question with caution. I'll just keep it as is for now. Thanks again. -Original Message- From: Duncan Murdoch [mailto:[EMAIL PROTECTED] Sent: Tuesday, October 24, 2006 3:05 PM To: Jerome Asselin Cc: Leeds, Mark (IED); R-help@stat.math.ethz.ch Subject: Re: [R] avoiding a loop On 10/24/2006 2:53 PM, Jerome Asselin wrote: On Tue, 2006-10-24 at 14:36 -0400, Leeds, Mark (IED) wrote: I think I asked a similar question 3 years ago to the Splus list and I think the answer was no or noone answered so noone should spend more than 5 minutes on this because it could definitely be a waste of time. My question is whether the function below can be rewritten without a for loop. apply is fine if it can be done that way but i doubt it. I call it a lot and would prefer to not loop. # --- -- constructLt-function(invector) { outvector-invector for ( i in 2:length(invector) ) { if ( invector[i] 1 ) { outvector[i]-invector[i]*outvector[i-1] } } return(outvector) } You sure can vectorize this. Try this below... I haven't tested, but it should be close to your solution. There's also a - invector[-1] outvector - invector wh - which(a1)+1 outvector[wh] - a[wh] * invector[-length(invector)][wh-1] outvector I think this misses the fact that if a sequence of invector values are less than 1, then the multiplication accumulates: e.g. with invector equal to rep(0.5, 5) the output should be constructLt(rep(0.5,5)) [1] 0.5 0.25000 0.12500 0.06250 0.03125 whereas your function gives asselin(rep(0.5,5)) [1] 0.50 0.25 0.25 0.25 NA A function like filter() might be able to do this, but in general R doesn't give a way to write recursive formulae in a simple vector form. In this particular case, I'd just use the for loop; if it turned out to be too slow, I'd rewrite it in C, in a nearly literal translation. Duncan Murdoch This is not an offer (or solicitation of an offer) to buy/se...{{dropped}} __ R-help@stat.math.ethz.ch 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 a loop
christos : change your vector to 1.0,1.0,0.5,2.0,0.2,0.4,4.0,5.0 You should get 1.00 1.00 0.50 2.00 0.40 0.16 4.00 5.00 But your function gives : 1.00 1.00 0.50 2.00 0.40 0.08 4.00 5.00 I think it doesn't work when there are two in a row elements that are less than 1.0. Again, I do appreciate your help. -Original Message- From: Christos Hatzis [mailto:[EMAIL PROTECTED] Sent: Tuesday, October 24, 2006 3:01 PM To: Leeds, Mark (IED); R-help@stat.math.ethz.ch Subject: RE: [R] avoiding a loop Try this (essentially the trick is to shift the invector to get the y[i-i] effect): constructLt-function(invector, a=1) { invector[invectora] - c(0,invector)[invectora] * invector[invectora] invector } aa - c(1,1,0.5,2,3,0.4,4,5) aa [1] 1.0 1.0 0.5 2.0 3.0 0.4 4.0 5.0 constructLt(aa) [1] 1.0 1.0 0.5 2.0 3.0 1.2 4.0 5.0 -Christos -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Leeds, Mark (IED) Sent: Tuesday, October 24, 2006 2:36 PM To: R-help@stat.math.ethz.ch Subject: [R] avoiding a loop I think I asked a similar question 3 years ago to the Splus list and I think the answer was no or noone answered so noone should spend more than 5 minutes on this because it could definitely be a waste of time. My question is whether the function below can be rewritten without a for loop. apply is fine if it can be done that way but i doubt it. I call it a lot and would prefer to not loop. #--- -- constructLt-function(invector) { outvector-invector for ( i in 2:length(invector) ) { if ( invector[i] 1 ) { outvector[i]-invector[i]*outvector[i-1] } } return(outvector) } #--- - This is not an offer (or solicitation of an offer) to buy/sell the securities/instruments mentioned or an official confirmation. Morgan Stanley may deal as principal in or own or act as market maker for securities/instruments mentioned or may advise the issuers. This is not research and is not from MS Research but it may refer to a research analyst/research report. Unless indicated, these views are the author's and may differ from those of Morgan Stanley research or others in the Firm. We do not represent this is accurate or complete and we may not update this. Past performance is not indicative of future returns. For additional information, research reports and important disclosures, contact me or see https://secure.ms.com/servlet/cls. You should not use e-mail to request, authorize or effect the purchase or sale of any security or instrument, to send transfer instructions, or to effect any other transactions. We cannot guarantee that any such requests received via ! e-mail will be processed in a timely manner. This communication is solely for the addressee(s) and may contain confidential information. We do not waive confidentiality by mistransmission. Contact me if you do not wish to receive these communications. In the UK, this communication is directed in the UK to those persons who are market counterparties or intermediate customers (as defined in the UK Financial Services Authority's rules). [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. This is not an offer (or solicitation of an offer) to buy/se...{{dropped}} __ R-help@stat.math.ethz.ch 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 a loop?
I have a vector, (not a list) repeated.measures.FACTOR.names [1] Insp1 Insp2 Insp3 Insp4 Insp5 Insp6 Insp7 Insp8 Insp9 and would like to convert this into a single string Insp1,Insp2,Insp3,Insp4,Insp5,Insp6,Insp7,Insp8,Insp9 I can do that with a loop, but isn't there a more elegant way? result - repeated.measures.FACTOR.names[[1]] for(i in 2:length(repeated.measures.FACTOR.names)) { result - paste(result, repeated.measures.FACTOR.names[[i]], sep=,) } result [1] Insp1,Insp2,Insp3,Insp4,Insp5,Insp6,Insp7,Insp8,Insp9 Thanks. Charles Annis, P.E. [EMAIL PROTECTED] phone: 561-352-9699 eFax: 614-455-3265 http://www.StatisticalEngineering.com __ R-help@stat.math.ethz.ch 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 a loop?
On Thu, 2006-10-12 at 12:43 -0400, Charles Annis, P.E. wrote: I have a vector, (not a list) repeated.measures.FACTOR.names [1] Insp1 Insp2 Insp3 Insp4 Insp5 Insp6 Insp7 Insp8 Insp9 and would like to convert this into a single string Insp1,Insp2,Insp3,Insp4,Insp5,Insp6,Insp7,Insp8,Insp9 I can do that with a loop, but isn't there a more elegant way? result - repeated.measures.FACTOR.names[[1]] for(i in 2:length(repeated.measures.FACTOR.names)) { result - paste(result, repeated.measures.FACTOR.names[[i]], sep=,) } result [1] Insp1,Insp2,Insp3,Insp4,Insp5,Insp6,Insp7,Insp8,Insp9 paste() is vectorized and note the use of 'collapse' in lieu of 'sep': paste(repeated.measures.FACTOR.names, collapse = ,) [1] Insp1,Insp2,Insp3,Insp4,Insp5,Insp6,Insp7,Insp8,Insp9 HTH, Marc Schwartz __ R-help@stat.math.ethz.ch 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 a loop?
Le Jeudi 12 Octobre 2006 12:43, Charles Annis, P.E. a écrit : I have a vector, (not a list) repeated.measures.FACTOR.names [1] Insp1 Insp2 Insp3 Insp4 Insp5 Insp6 Insp7 Insp8 Insp9 and would like to convert this into a single string Insp1,Insp2,Insp3,Insp4,Insp5,Insp6,Insp7,Insp8,Insp9 I can do that with a loop, but isn't there a more elegant way? result - repeated.measures.FACTOR.names[[1]] for(i in 2:length(repeated.measures.FACTOR.names)) { result - paste(result, repeated.measures.FACTOR.names[[i]], sep=,) } result [1] Insp1,Insp2,Insp3,Insp4,Insp5,Insp6,Insp7,Insp8,Insp9 Thanks. Charles Annis, P.E. [EMAIL PROTECTED] phone: 561-352-9699 eFax: 614-455-3265 http://www.StatisticalEngineering.com paste() will do what you want. -- Vincent Goulet, Professeur agrégé École d'actuariat Université Laval, Québec [EMAIL PROTECTED] http://vgoulet.act.ulaval.ca __ R-help@stat.math.ethz.ch 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 a loop?
On Thu, 2006-10-12 at 12:07 -0500, Marc Schwartz wrote: On Thu, 2006-10-12 at 12:43 -0400, Charles Annis, P.E. wrote: I have a vector, (not a list) repeated.measures.FACTOR.names [1] Insp1 Insp2 Insp3 Insp4 Insp5 Insp6 Insp7 Insp8 Insp9 and would like to convert this into a single string Insp1,Insp2,Insp3,Insp4,Insp5,Insp6,Insp7,Insp8,Insp9 I can do that with a loop, but isn't there a more elegant way? result - repeated.measures.FACTOR.names[[1]] for(i in 2:length(repeated.measures.FACTOR.names)) { result - paste(result, repeated.measures.FACTOR.names[[i]], sep=,) } result [1] Insp1,Insp2,Insp3,Insp4,Insp5,Insp6,Insp7,Insp8,Insp9 paste() is vectorized and note the use of 'collapse' in lieu of 'sep': paste(repeated.measures.FACTOR.names, collapse = ,) [1] Insp1,Insp2,Insp3,Insp4,Insp5,Insp6,Insp7,Insp8,Insp9 Before I forget, you can also do the following to reconstruct the initial sequence and the final result in a single step: paste(Insp, 1:9, sep = , collapse = ,) [1] Insp1,Insp2,Insp3,Insp4,Insp5,Insp6,Insp7,Insp8,Insp9 In this case, we use 'sep' to indicate that there should be no space between each occurrence of 'Insp' and the integers and then use 'collapse' to indicate (as above) that each alphanum construct is to be joined by a comma into a single element. HTH, Marc __ R-help@stat.math.ethz.ch 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 for loop
I understand that in R, for loops are not used as often as other languages, and am trying to learn how to avoid them. I am wondering if there is a more efficient way to write a certain piece of code, which right now I can only envision as a for loop. I have a data file that basically looks like: 1,55 1,23 2,12 ... that defines a matrix. Each row of the data file corresponds to a row of the matrix, where each number in the row tells me what column a 1 or -1 should go into. So the first row in the data snippet above means that the first row of my matrix needs to have a 1 in the 1st column, and a -1 in the 55nd column. (And 0 elsewhere, which is already there as I've created the matrix filled with 0s beforehand.) So my current code looks like: if(nrow(rawdata) = 1) for(i in 1:nrow(rawdata)) { X[i, rawdata[i, 1]] - 1 X[i, rawdata[i, 2]] - -1 } where rawdata is the original data file. This sort of assignment happens many times in my program so any improvement would be much appreciated. Thanks. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Avoiding for loop
On Thu, 4 Aug 2005, Matt Crawford wrote: I understand that in R, for loops are not used as often as other languages, and am trying to learn how to avoid them. I am wondering if there is a more efficient way to write a certain piece of code, which right now I can only envision as a for loop. I have a data file that basically looks like: 1,55 1,23 2,12 ... that defines a matrix. Each row of the data file corresponds to a row of the matrix, where each number in the row tells me what column a 1 or -1 should go into. So the first row in the data snippet above means that the first row of my matrix needs to have a 1 in the 1st column, and a -1 in the 55nd column. (And 0 elsewhere, which is already there as I've created the matrix filled with 0s beforehand.) This may be a job for matrix indexes ii-1:nrow(rawdata) X[cbind(ii,rawdata[,1])] - 1 X[cbind(ii,rawdata[,2])] - -1 -thomas __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Avoiding for loop
On 8/4/05, Matt Crawford [EMAIL PROTECTED] wrote: I understand that in R, for loops are not used as often as other languages, and am trying to learn how to avoid them. I am wondering if there is a more efficient way to write a certain piece of code, which right now I can only envision as a for loop. I have a data file that basically looks like: 1,55 1,23 2,12 ... that defines a matrix. Each row of the data file corresponds to a row of the matrix, where each number in the row tells me what column a 1 or -1 should go into. So the first row in the data snippet above means that the first row of my matrix needs to have a 1 in the 1st column, and a -1 in the 55nd column. (And 0 elsewhere, which is already there as I've created the matrix filled with 0s beforehand.) So my current code looks like: if(nrow(rawdata) = 1) for(i in 1:nrow(rawdata)) { X[i, rawdata[i, 1]] - 1 X[i, rawdata[i, 2]] - -1 } where rawdata is the original data file. This sort of assignment happens many times in my program so any improvement would be much appreciated. Thanks. idx - seq(length = max(rawdata, 0)) X - outer(rawdata[,1], idx, ==) - outer(rawdata[,2], idx, ==) Note that we did not have to predefine X and it also works if rawdata has zero rows: rawdata - matrix(0, nr = 0, nc = 2) in which case it gives a 0 by 0 matrix. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Avoiding a Loop?
Dear R-Helpers, I have a matrix where the first column is known. The second column is the result of multiplying this first column with a constant const. The third column is the result of multiplying the second column with const. So far, I did it like this (as a simplified example): nr.of.columns - 4 myconstant - 27.5 mymatrix - matrix(numeric(0), nrow=5, ncol=nr.of.columns) mymatrix[,1] - 1:5 for (i in 2:nr.of.columns) { mymatrix[,i] - myconstant * mymatrix[,i-1] } Can anyone give me some advice whether it is possible to avoid this loop (and if yes: how)? Any suggestions are welcome! Thanks, Roland + This mail has been sent through the MPI for Demographic Rese...{{dropped}} __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] Avoiding a Loop?
If `v' the the vector in the first column and `k' is the constant, then the matrix has k^(j-1) * v in the jth column, right? k - 27.5^seq(0, length=4) k [1] 1.0027.50 756.25 20796.88 outer(1:5, k, *) [,1] [,2][,3] [,4] [1,]1 27.5 756.25 20796.88 [2,]2 55.0 1512.50 41593.75 [3,]3 82.5 2268.75 62390.63 [4,]4 110.0 3025.00 83187.50 [5,]5 137.5 3781.25 103984.38 Andy From: Rau, Roland Dear R-Helpers, I have a matrix where the first column is known. The second column is the result of multiplying this first column with a constant const. The third column is the result of multiplying the second column with const. So far, I did it like this (as a simplified example): nr.of.columns - 4 myconstant - 27.5 mymatrix - matrix(numeric(0), nrow=5, ncol=nr.of.columns) mymatrix[,1] - 1:5 for (i in 2:nr.of.columns) { mymatrix[,i] - myconstant * mymatrix[,i-1] } Can anyone give me some advice whether it is possible to avoid this loop (and if yes: how)? Any suggestions are welcome! Thanks, Roland + This mail has been sent through the MPI for Demographic Rese...{{dropped}} __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Avoiding a Loop?
Does this do what you want? nr.of.columns - 4 myconstant - 27.5 mymatrix - matrix(myconstant, nrow=5, ncol=nr.of.columns) mymatrix[,1] - 1:5 t(apply(mymatrix, 1, function(x) cumprod(x))) __ James HoltmanWhat is the problem you are trying to solve? Executive Technical Consultant -- Office of Technology, Convergys [EMAIL PROTECTED] +1 (513) 723-2929 Rau, Roland [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Sent by: cc: [EMAIL PROTECTED]Subject: [R] Avoiding a Loop? ath.ethz.ch 01/21/2005 07:31 Dear R-Helpers, I have a matrix where the first column is known. The second column is the result of multiplying this first column with a constant const. The third column is the result of multiplying the second column with const. So far, I did it like this (as a simplified example): nr.of.columns - 4 myconstant - 27.5 mymatrix - matrix(numeric(0), nrow=5, ncol=nr.of.columns) mymatrix[,1] - 1:5 for (i in 2:nr.of.columns) { mymatrix[,i] - myconstant * mymatrix[,i-1] } Can anyone give me some advice whether it is possible to avoid this loop (and if yes: how)? Any suggestions are welcome! Thanks, Roland + This mail has been sent through the MPI for Demographic Rese...{{dropped}} __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] Avoiding a Loop?
Dear R-List, thank you very much for the fast answers. Andy Liaw and James Holtman both gave me some example code which does exactly what I want. I will now try to check which one is better suited for my actual matrix (in terms of speed). Thanks, Roland -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Friday, January 21, 2005 3:57 PM To: Rau, Roland Cc: r-help@stat.math.ethz.ch; [EMAIL PROTECTED] Subject: Re: [R] Avoiding a Loop? Does this do what you want? nr.of.columns - 4 myconstant - 27.5 mymatrix - matrix(myconstant, nrow=5, ncol=nr.of.columns) mymatrix[,1] - 1:5 t(apply(mymatrix, 1, function(x) cumprod(x))) __ James HoltmanWhat is the problem you are trying to solve? Executive Technical Consultant -- Office of Technology, Convergys [EMAIL PROTECTED] +1 (513) 723-2929 Rau, Roland [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Sent by: cc: [EMAIL PROTECTED]Subject: [R] Avoiding a Loop? ath.ethz.ch 01/21/2005 07:31 Dear R-Helpers, I have a matrix where the first column is known. The second column is the result of multiplying this first column with a constant const. The third column is the result of multiplying the second column with const. So far, I did it like this (as a simplified example): nr.of.columns - 4 myconstant - 27.5 mymatrix - matrix(numeric(0), nrow=5, ncol=nr.of.columns) mymatrix[,1] - 1:5 for (i in 2:nr.of.columns) { mymatrix[,i] - myconstant * mymatrix[,i-1] } Can anyone give me some advice whether it is possible to avoid this loop (and if yes: how)? Any suggestions are welcome! Thanks, Roland + This mail has been sent through the MPI for Demographic Rese...{{dropped}} __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html + This mail has been sent through the MPI for Demographic Rese...{{dropped}} __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Avoiding a Loop?
Rau, Roland [EMAIL PROTECTED] writes: Dear R-Helpers, I have a matrix where the first column is known. The second column is the result of multiplying this first column with a constant const. The third column is the result of multiplying the second column with const. So far, I did it like this (as a simplified example): nr.of.columns - 4 myconstant - 27.5 mymatrix - matrix(numeric(0), nrow=5, ncol=nr.of.columns) mymatrix[,1] - 1:5 for (i in 2:nr.of.columns) { mymatrix[,i] - myconstant * mymatrix[,i-1] } Can anyone give me some advice whether it is possible to avoid this loop (and if yes: how)? How about: myconstant - 27.5 a - 1:5 myconstant - myconstant^((1:nr.of.columns)-1) myconstant [1] 1.0027.50 756.25 20796.88 mymatrix - outer(a,myconstant) mymatrix [,1] [,2][,3] [,4] [1,]1 27.5 756.25 20796.88 [2,]2 55.0 1512.50 41593.75 [3,]3 82.5 2268.75 62390.62 [4,]4 110.0 3025.00 83187.50 [5,]5 137.5 3781.25 103984.38 HTH Zoltan Any suggestions are welcome! Thanks, Roland + This mail has been sent through the MPI for Demographic Rese...{{dropped}} __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- Z. Barta Dept. of Evol. Zool., Univ. of Debrecen, Debrecen, H-4010, Hungary Phone: 36 52 316 666 ext. 2334, Fax: 36 52 533 677 E-mail: [EMAIL PROTECTED], http://puma.unideb.hu/~zbarta/ __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Avoiding a Loop?
Rau, Roland wrote: Dear R-Helpers, I have a matrix where the first column is known. The second column is the result of multiplying this first column with a constant const. The third column is the result of multiplying the second column with const. So far, I did it like this (as a simplified example): nr.of.columns - 4 myconstant - 27.5 mymatrix - matrix(numeric(0), nrow=5, ncol=nr.of.columns) mymatrix[,1] - 1:5 for (i in 2:nr.of.columns) { mymatrix[,i] - myconstant * mymatrix[,i-1] } nr.of.columns - 4 myconstant - 27.5 mycolumn - 1:5 outer(mycolumn, myconstant^(0:(nr.of.columns-1))) Uwe Ligges Can anyone give me some advice whether it is possible to avoid this loop (and if yes: how)? Any suggestions are welcome! Thanks, Roland + This mail has been sent through the MPI for Demographic Rese...{{dropped}} __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] Avoiding a Loop?
Roland: Andy Liaw and others have already given perfectly good answers to this (but note: Using apply() type functions does **not** avoid loops; apply's **are** loops). However, mostly as an illustration to reinforce Uwe Ligges's comments (in the dim vs length thread) about the usefulness of sometimes treating arrays as vectors, I offer the following: nc-ncol(yourmatrix) matrix(yourmatrix[,1]*rep(k^seq(0,length=nc),e=nrow(yourmatrix)),ncol=nc) Alteratively, one could use matrix multiplication: yourmatrix[,1]%*%matrix(k^seq(0,length=nc),nrow=1) Both of these will be very fast (although unlikely to make a noticeable difference without a lot of rows or columns) and I think use less memory than outer() (again, unlikely to make a noticeable difference). -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Rau, Roland Sent: Friday, January 21, 2005 4:32 AM To: r-help@stat.math.ethz.ch Subject: [R] Avoiding a Loop? Dear R-Helpers, I have a matrix where the first column is known. The second column is the result of multiplying this first column with a constant const. The third column is the result of multiplying the second column with const. So far, I did it like this (as a simplified example): nr.of.columns - 4 myconstant - 27.5 mymatrix - matrix(numeric(0), nrow=5, ncol=nr.of.columns) mymatrix[,1] - 1:5 for (i in 2:nr.of.columns) { mymatrix[,i] - myconstant * mymatrix[,i-1] } Can anyone give me some advice whether it is possible to avoid this loop (and if yes: how)? Any suggestions are welcome! Thanks, Roland + This mail has been sent through the MPI for Demographic Rese...{{dropped}} __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html