I have run into a speed issue, and given the size of the problem it feels like there should be an easy solution. Here is the problem statement with some arbitrary numbers added. #p,q: vector with length(q)==length(p)==10000 and length(levels(p))==3000 #y,z: vectors with length(levels(y))=length(y)==length(z)==5000 for (i in levels(p)){ q[i==p]<-z[i==y]} At first i used two for loops which was horrible, now i got rid of one but i don't know how to lose the second one. PS. I expect the solution to be available through google etc and I have searched for a solution but i did not find any usefull websites, probably because i cannot pinpoint the best search words. -- View this message in context: http://r.789695.n4.nabble.com/R-loop-problem-tp3718103p3718103.html Sent from the R help mailing list archive at Nabble.com.
A subset of actual data and what you would expect as a result would be very helpful. All you say is that p.q are vectors, but it would appear that they are character vectors, but the content is unknown. Also will the expression "q[i==p]<-z[i==y]" have the same length on each side; the vectors appear to be of different lengths -- what happens if recycling kicks in? Or it is always a match of length 1" So a little more definition, and data and example, would help. On Thu, Aug 4, 2011 at 5:27 AM, testrider <testrider at gmail.com> wrote:> I have run into a speed issue, and given the size of the problem it feels > like there should be an easy solution. Here is the problem statement with > some arbitrary numbers added. > > #p,q: vector with length(q)==length(p)==10000 and length(levels(p))==3000 > #y,z: vectors with length(levels(y))=length(y)==length(z)==5000 > > for (i in levels(p)){ > q[i==p]<-z[i==y]} > > At first i used two for loops which was horrible, now i got rid of one but i > don't know how to lose the second one. > > > PS. I expect the solution to be available through google etc and I have > searched for a solution but i did not find any usefull websites, probably > because i cannot pinpoint the best search words. > > -- > View this message in context: http://r.789695.n4.nabble.com/R-loop-problem-tp3718103p3718103.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
Try this: q <- z[match(p, y)] Jean `·.,, ><(((º> `·.,, ><(((º> `·.,, ><(((º> Jean V. Adams Statistician U.S. Geological Survey Great Lakes Science Center 223 East Steinfest Road Antigo, WI 54409 USA 715-627-4317, ext. 3125 (Office) 715-216-8014 (Cell) 715-623-6773 (FAX) http://www.glsc.usgs.gov (GLSC web site) http://profile.usgs.gov/jvadams (My homepage) jvadams@usgs.gov (E-mail) From: testrider <testrider@gmail.com> To: r-help@r-project.org Date: 08/04/2011 07:21 AM Subject: [R] R loop problem Sent by: r-help-bounces@r-project.org I have run into a speed issue, and given the size of the problem it feels like there should be an easy solution. Here is the problem statement with some arbitrary numbers added. #p,q: vector with length(q)==length(p)==10000 and length(levels(p))==3000 #y,z: vectors with length(levels(y))=length(y)==length(z)==5000 for (i in levels(p)){ q[i==p]<-z[i==y]} At first i used two for loops which was horrible, now i got rid of one but i don't know how to lose the second one. PS. I expect the solution to be available through google etc and I have searched for a solution but i did not find any usefull websites, probably because i cannot pinpoint the best search words. -- View this message in context: http://r.789695.n4.nabble.com/R-loop-problem-tp3718103p3718103.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. [[alternative HTML version deleted]]