|
I am creating a nested for loop and following are the codes I'm using, but I am not acheiving what I want.
I have a vector d<-151:159 I have another vector e<-e<-c("apple", "orange", "banana") I need to create f as 151apple 151orange 151banana . . 159apple 159orange 159banana Here is how I wrote nested for loop... for (i in 1:length(d)) { for (j in 1:length(e)) { x[j]<-paste(d[i],e[j],sep="") print(x[j]) } } The result of the above codes is.... > for (i in 1:length(d)) + { for (j in 1:length(e)) + { + x[j]<-paste(d[i],e[j],sep="") + print(x[j]) + } + } [1] "151apple" [1] "151orange" [1] "151banana" > What do I need to do this looping produce the desired result. Thanks Arun ---- |
|
Hello,
Try, in one line and no loops, surely not nested, apply(rev(expand.grid(e, d, stringsAsFactors = FALSE)), 1, paste, collapse="") Hope this helps, Rui Barradas Em 28-06-2012 22:03, arun.gurubaramurugeshan escreveu: > I am creating a nested for loop and following are the codes I'm using, but I > am not acheiving what I want. > > I have a vector > > d<-151:159 > > I have another vector > > e<-e<-c("apple", "orange", "banana") > > I need to create f as > 151apple > 151orange > 151banana > . > . > 159apple > 159orange > 159banana > > Here is how I wrote nested for loop... > > for (i in 1:length(d)) > { for (j in 1:length(e)) > { > x[j]<-paste(d[i],e[j],sep="") > print(x[j]) > } > } > > The result of the above codes is.... > >> for (i in 1:length(d)) > + { for (j in 1:length(e)) > + { > + x[j]<-paste(d[i],e[j],sep="") > + print(x[j]) > + } > + } > [1] "151apple" > [1] "151orange" > [1] "151banana" >> > > > What do I need to do this looping produce the desired result. > > Thanks > Arun > ---- > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Nested-For-Loop-tp4634804.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. > ______________________________________________ [hidden email] 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. |
|
... or even simpler:
levels(interaction(e,d,sep="")) ... and PLEASE read "An Introduction to R" before further posting to learn how to program in R rather than trying to fit what you do in other languages to R. Cheers, Bert On Fri, Jun 29, 2012 at 12:48 AM, Rui Barradas <[hidden email]> wrote: > Hello, > > Try, in one line and no loops, surely not nested, > > apply(rev(expand.grid(e, d, stringsAsFactors = FALSE)), 1, paste, > collapse="") > > Hope this helps, > > Rui Barradas > > Em 28-06-2012 22:03, arun.gurubaramurugeshan escreveu: > >> I am creating a nested for loop and following are the codes I'm using, >> but I >> am not acheiving what I want. >> >> I have a vector >> >> d<-151:159 >> >> I have another vector >> >> e<-e<-c("apple", "orange", "banana") >> >> I need to create f as >> 151apple >> 151orange >> 151banana >> . >> . >> 159apple >> 159orange >> 159banana >> >> Here is how I wrote nested for loop... >> >> for (i in 1:length(d)) >> { for (j in 1:length(e)) >> { >> x[j]<-paste(d[i],e[j],sep="") >> print(x[j]) >> } >> } >> >> The result of the above codes is.... >> >> for (i in 1:length(d)) >>> >> + { for (j in 1:length(e)) >> + { >> + x[j]<-paste(d[i],e[j],sep="") >> + print(x[j]) >> + } >> + } >> [1] "151apple" >> [1] "151orange" >> [1] "151banana" >> >>> >>> >> >> What do I need to do this looping produce the desired result. >> >> Thanks >> Arun >> ---- >> >> >> >> >> >> -- >> View this message in context: http://r.789695.n4.nabble.com/** >> Nested-For-Loop-tp4634804.html<http://r.789695.n4.nabble.com/Nested-For-Loop-tp4634804.html> >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________**________________ >> [hidden email] mailing list >> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> >> PLEASE do read the posting guide http://www.R-project.org/** >> posting-guide.html <http://www.R-project.org/posting-guide.html> >> and provide commented, minimal, self-contained, reproducible code. >> >> > ______________________________**________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> > PLEASE do read the posting guide http://www.R-project.org/** > posting-guide.html <http://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[alternative HTML version deleted]] ______________________________________________ [hidden email] 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. |
|
reversed them, Should be:
levels(interaction(d,e,sep="") > > of course. -- Bert On Fri, Jun 29, 2012 at 2:46 AM, Bert Gunter <[hidden email]> wrote: > ... or even simpler: > > levels(interaction(e,d,sep="")) > > ... and PLEASE read "An Introduction to R" before further posting to learn > how to program in R rather than trying to fit what you do in other > languages to R. > > Cheers, > Bert > > On Fri, Jun 29, 2012 at 12:48 AM, Rui Barradas <[hidden email]>wrote: > >> Hello, >> >> Try, in one line and no loops, surely not nested, >> >> apply(rev(expand.grid(e, d, stringsAsFactors = FALSE)), 1, paste, >> collapse="") >> >> Hope this helps, >> >> Rui Barradas >> >> Em 28-06-2012 22:03, arun.gurubaramurugeshan escreveu: >> >>> I am creating a nested for loop and following are the codes I'm using, >>> but I >>> am not acheiving what I want. >>> >>> I have a vector >>> >>> d<-151:159 >>> >>> I have another vector >>> >>> e<-e<-c("apple", "orange", "banana") >>> >>> I need to create f as >>> 151apple >>> 151orange >>> 151banana >>> . >>> . >>> 159apple >>> 159orange >>> 159banana >>> >>> Here is how I wrote nested for loop... >>> >>> for (i in 1:length(d)) >>> { for (j in 1:length(e)) >>> { >>> x[j]<-paste(d[i],e[j],sep="") >>> print(x[j]) >>> } >>> } >>> >>> The result of the above codes is.... >>> >>> for (i in 1:length(d)) >>>> >>> + { for (j in 1:length(e)) >>> + { >>> + x[j]<-paste(d[i],e[j],sep="") >>> + print(x[j]) >>> + } >>> + } >>> [1] "151apple" >>> [1] "151orange" >>> [1] "151banana" >>> >>>> >>>> >>> >>> What do I need to do this looping produce the desired result. >>> >>> Thanks >>> Arun >>> ---- >>> >>> >>> >>> >>> >>> -- >>> View this message in context: http://r.789695.n4.nabble.com/** >>> Nested-For-Loop-tp4634804.html<http://r.789695.n4.nabble.com/Nested-For-Loop-tp4634804.html> >>> Sent from the R help mailing list archive at Nabble.com. >>> >>> ______________________________**________________ >>> [hidden email] mailing list >>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> >>> PLEASE do read the posting guide http://www.R-project.org/** >>> posting-guide.html <http://www.R-project.org/posting-guide.html> >>> and provide commented, minimal, self-contained, reproducible code. >>> >>> >> ______________________________**________________ >> [hidden email] mailing list >> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> >> PLEASE do read the posting guide http://www.R-project.org/** >> posting-guide.html <http://www.R-project.org/posting-guide.html> >> and provide commented, minimal, self-contained, reproducible code. >> > > > > -- > > Bert Gunter > Genentech Nonclinical Biostatistics > > Internal Contact Info: > Phone: 467-7374 > Website: > > http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm > > > -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[alternative HTML version deleted]] ______________________________________________ [hidden email] 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. |
|
In reply to this post by arun.gurubaramurugeshan
On Jun 28, 2012, at 5:03 PM, arun.gurubaramurugeshan wrote: > I am creating a nested for loop and following are the codes I'm > using, but I > am not acheiving what I want. > > I have a vector > > d<-151:159 > > I have another vector > > e<-e<-c("apple", "orange", "banana") > > I need to create f as > 151apple > 151orange > 151banana > . > . > 159apple > 159orange > 159banana > I admit that I think Bert's solution is way kewler than mine, but I would not have thought of using interaction() to mimic the "crossed" use of rep() paste(as.character(rep(d, each=length(e))), rep(e, times=length(d) ), sep="") (Looking at the code for interaction one sees that this is how it was coded.) The other R functions to remember for this sort of loop-avoidance are 'outer' and 'expand.grid'. > outer(d,e,paste, sep="") [,1] [,2] [,3] [1,] "151apple" "151orange" "151banana" [2,] "152apple" "152orange" "152banana" [3,] "153apple" "153orange" "153banana" [4,] "154apple" "154orange" "154banana" [5,] "155apple" "155orange" "155banana" as.vector( outer(d, e, paste, sep="") ) -- David. > Here is how I wrote nested for loop... > > for (i in 1:length(d)) > { for (j in 1:length(e)) > { > x[j]<-paste(d[i],e[j],sep="") > print(x[j]) > } > } > > The result of the above codes is.... > >> for (i in 1:length(d)) > + { for (j in 1:length(e)) > + { > + x[j]<-paste(d[i],e[j],sep="") > + print(x[j]) > + } > + } > [1] "151apple" > [1] "151orange" > [1] "151banana" >> > > > What do I need to do this looping produce the desired result. > > Thanks > Arun > ---- > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Nested-For-Loop-tp4634804.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. David Winsemius, MD West Hartford, CT ______________________________________________ [hidden email] 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. |
|
In reply to this post by arun.gurubaramurugeshan
Hi,
I knew that you already got many replies. Here is my contribution. dat1<-paste0(expand.grid(d,e)$Var1,expand.grid(d,e)$Var2,sep="") or dat2<-as.vector(rbind(paste0(d,e[1],sep=""),paste0(d,e[2],sep=""),paste0(d,e[3],sep=""))) #Still Bert's one line is the shortest A.K. ----- Original Message ----- From: arun.gurubaramurugeshan <[hidden email]> To: [hidden email] Cc: Sent: Thursday, June 28, 2012 5:03 PM Subject: [R] Nested For Loop I am creating a nested for loop and following are the codes I'm using, but I am not acheiving what I want. I have a vector d<-151:159 I have another vector e<-e<-c("apple", "orange", "banana") I need to create f as 151apple 151orange 151banana . . 159apple 159orange 159banana Here is how I wrote nested for loop... for (i in 1:length(d)) { for (j in 1:length(e)) { x[j]<-paste(d[i],e[j],sep="") print(x[j]) } } The result of the above codes is.... > for (i in 1:length(d)) + { for (j in 1:length(e)) + { + x[j]<-paste(d[i],e[j],sep="") + print(x[j]) + } + } [1] "151apple" [1] "151orange" [1] "151banana" > What do I need to do this looping produce the desired result. Thanks Arun ---- -- View this message in context: http://r.789695.n4.nabble.com/Nested-For-Loop-tp4634804.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ [hidden email] 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. ______________________________________________ [hidden email] 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. |
|
Don't (can't) use paste0 with sep='' -- that's redundant, and I'm surprised not an error.
Michael On Jun 29, 2012, at 7:18 PM, arun <[hidden email]> wrote: > Hi, > > I knew that you already got many replies. > > Here is my contribution. > dat1<-paste0(expand.grid(d,e)$Var1,expand.grid(d,e)$Var2,sep="") > > or > dat2<-as.vector(rbind(paste0(d,e[1],sep=""),paste0(d,e[2],sep=""),paste0(d,e[3],sep=""))) > > > #Still Bert's one line is the shortest > > > A.K. > > > > ----- Original Message ----- > From: arun.gurubaramurugeshan <[hidden email]> > To: [hidden email] > Cc: > Sent: Thursday, June 28, 2012 5:03 PM > Subject: [R] Nested For Loop > > I am creating a nested for loop and following are the codes I'm using, but I > am not acheiving what I want. > > I have a vector > > d<-151:159 > > I have another vector > > e<-e<-c("apple", "orange", "banana") > > I need to create f as > 151apple > 151orange > 151banana > . > . > 159apple > 159orange > 159banana > > Here is how I wrote nested for loop... > > for (i in 1:length(d)) > { for (j in 1:length(e)) > { > x[j]<-paste(d[i],e[j],sep="") > print(x[j]) > } > } > > The result of the above codes is.... > >> for (i in 1:length(d)) > + { for (j in 1:length(e)) > + { > + x[j]<-paste(d[i],e[j],sep="") > + print(x[j]) > + } > + } > [1] "151apple" > [1] "151orange" > [1] "151banana" >> > > > What do I need to do this looping produce the desired result. > > Thanks > Arun > ---- > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Nested-For-Loop-tp4634804.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
| Powered by Nabble | Edit this page |
