jiliguala
2011-Jun-12 16:17 UTC
[R] automatically generate the output name of my "for" loops
Hello R users, I am new to R and am having difficulty with the output name of my "for" loops. here is the problem: for (i in c(1:100)) { the name of the groups <- which(k1$cluster==i) } how can it automatically generate the name for 100 cluster? what should i put in the bold letter place. really thank you for helping me Daniel -- View this message in context: http://r.789695.n4.nabble.com/automatically-generate-the-output-name-of-my-for-loops-tp3592160p3592160.html Sent from the R help mailing list archive at Nabble.com.
?paste something like... paste (group, i, sep="_") jiliguala wrote:> > Hello R users, > > I am new to R and am having difficulty with the output name of my "for" > loops. > > here is the problem: > > > for (i in c(1:100)) > { > the name of the groups <- which(k1$cluster==i) > } > > how can it automatically generate the name for 100 cluster(just like > group_1, group_2...)? what should i put in the bold letter place? > really thank you for helping me > > Daniel >-- View this message in context: http://r.789695.n4.nabble.com/automatically-generate-the-output-name-of-my-for-loops-tp3592160p3593123.html Sent from the R help mailing list archive at Nabble.com.
?paste something like... paste ("group", i, sep="_") jiliguala wrote:> > Hello R users, > > I am new to R and am having difficulty with the output name of my "for" > loops. > > here is the problem: > > > for (i in c(1:100)) > { > the name of the groups <- which(k1$cluster==i) > } > > how can it automatically generate the name for 100 cluster(just like > group_1, group_2...)? what should i put in the bold letter place? > really thank you for helping me > > Daniel >-- View this message in context: http://r.789695.n4.nabble.com/automatically-generate-the-output-name-of-my-for-loops-tp3592160p3593124.html Sent from the R help mailing list archive at Nabble.com.
Petr PIKAL
2011-Jun-13 08:51 UTC
[R] automatically generate the output name of my "for" loops
Hi r-help-bounces at r-project.org napsal dne 13.06.2011 05:21:23:> Re: [R] automatically generate the output name of my "for" loops > > ?paste > something like... > paste (group, i, sep="_") >I believe that better idea is to use list, with which you can work further much more efficiently. lll<- vector("list", 100) for (i in 1:100) { lll[i] <- some computation } Regards Petr> > > jiliguala wrote: > > > > Hello R users, > > > > I am new to R and am having difficulty with the output name of my"for"> > loops. > > > > here is the problem: > > > > > > for (i in c(1:100)) > > { > > the name of the groups <- which(k1$cluster==i) > > } > > > > how can it automatically generate the name for 100 cluster(just like > > group_1, group_2...)? what should i put in the bold letter place? > > really thank you for helping me > > > > Daniel > > > > -- > View this message in context:http://r.789695.n4.nabble.com/automatically-> generate-the-output-name-of-my-for-loops-tp3592160p3593123.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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
jiliguala
2011-Jun-13 15:35 UTC
[R] automatically generate the output name of my "for" loops
really thanks for helping. i just did just like what Petr Pikal said, but it appeared some error like this: There were 50 or more warnings (use warnings() to see the first 50): Warning messages: 1: In group[i] <- which(k1$cluster == i) : number of items to replace is not a multiple of replacement length 2: In group[i] <- which(k1$cluster == i) : number of items to replace is not a multiple of replacement length 3: In group[i] <- which(k1$cluster == i) : number of items to replace is not a multiple of replacement length ... ... ... and some solution for that, thanks.. -- View this message in context: http://r.789695.n4.nabble.com/automatically-generate-the-output-name-of-my-for-loops-tp3592160p3594096.html Sent from the R help mailing list archive at Nabble.com.
Vijayanpadmanabhan
2011-Jun-13 15:55 UTC
[R] automatically generate the output name of my "for" loops
Hi Daniel Try using this approach.... ##Drawing analogy from your example.. I am storing the values generated in a loop in the following code for (i in c(1:100)) { nam <- paste("r",i, sep=".") assign(nam, i+1) } ##Having stored the values generated in the above loop in as many R objects..Here is how you extract them.. for (i in c(1:100)) { print(get(paste("r",i, sep=".")) ) } Trust it helps. Regards Vijayan Padmanabhan -- View this message in context: http://r.789695.n4.nabble.com/automatically-generate-the-output-name-of-my-for-loops-tp3592160p3594160.html Sent from the R help mailing list archive at Nabble.com.
jim holtman
2011-Jun-13 17:31 UTC
[R] automatically generate the output name of my "for" loops
If you want to have multidimensioned list, here is one way of doing it by making sure you initialize the second level before using it: data2 <- matrix(1:30, nrow = 10) data3 <- matrix(1:300, nrow = 100) data1 <- list() # init to a list for (j in 1:10){ data1[[j]] <- list() # create second level list for (i in 1:100){ data1[[j]][[i]] <- rbind(data2[j, ], data3[i, ]) } } notice the reversal of the indices. On Mon, Jun 13, 2011 at 12:29 PM, jiliguala <jiliguala at mail.com> wrote:> hi, really thank u, Petr Pikal > > problem solved already. > > but here is another question: > > for(i in 1:100) > data1[[i]] <- rbind(data2[1,], data3[i,]) > > the codes above are no problem, > but now i wnna do two loops like the codes below which have problem(the bold > letters): > i dont know what should be put in the bold letters place. > > for (j in 1:10) > for (i in 1:100) > data1[[i]][[j]] <- rbind(data2[j,], data3[i,]) > > > thanks for helping. > > daniel > > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/automatically-generate-the-output-name-of-my-for-loops-tp3592160p3594243.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?
jiliguala
2011-Jun-13 17:45 UTC
[R] automatically generate the output name of my "for" loops
hi, jholtman its true, the problem was to iniciate the list at the second level. thx a lot -- View this message in context: http://r.789695.n4.nabble.com/automatically-generate-the-output-name-of-my-for-loops-tp3592160p3594397.html Sent from the R help mailing list archive at Nabble.com.
Greg Snow
2011-Jun-14 21:43 UTC
[R] automatically generate the output name of my "for" loops
This is actually FAQ 7.21. As others have mentioned, the most important part of the Answer is that it is better to use a list instead. What searching did you do before posting? Is there some way that the FAQ could be changed that would have made your searching turn up the FAQ answer? -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of jiliguala > Sent: Sunday, June 12, 2011 10:18 AM > To: r-help at r-project.org > Subject: [R] automatically generate the output name of my "for" loops > > > Hello R users, > > I am new to R and am having difficulty with the output name of my "for" > loops. > > here is the problem: > > > for (i in c(1:100)) > { > the name of the groups <- which(k1$cluster==i) > } > > how can it automatically generate the name for 100 cluster? what should > i > put in the bold letter place. > > really thank you for helping me > > Daniel > > -- > View this message in context: > http://r.789695.n4.nabble.com/automatically-generate-the-output-name- > of-my-for-loops-tp3592160p3592160.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.
jiliguala
2011-Jun-14 22:14 UTC
[R] automatically generate the output name of my "for" loops
hi, im a new in R and this forum, so if there is something disturbing u, im gonna do it better. by the way, where can i see the FAQ 7.21?? thanks.. -- View this message in context: http://r.789695.n4.nabble.com/automatically-generate-the-output-name-of-my-for-loops-tp3592160p3597987.html Sent from the R help mailing list archive at Nabble.com.
Greg Snow
2011-Jun-14 22:27 UTC
[R] automatically generate the output name of my "for" loops
If the list is simple enough, then you probably want the "unlist" function, otherwise you will probably need to use the "sapply" function to grab and/or process the important pieces in each list element. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of jiliguala > Sent: Tuesday, June 14, 2011 4:24 PM > To: r-help at r-project.org > Subject: Re: [R] automatically generate the output name of my "for" > loops > > > thanks, Greg Snow-2. > > here i hav a question, how can i save the value of a list to a vector > or a > matrix? say, > i have a list (data1) and a vector or a matrix (data2). > > if i made like this, > > data2 <- as.vector(data1) > > it does not change anything, the data2 is still a list. > > what i want to do is to creat a vector or a matrix to save the value of > the > list. > > thanks for helping > > daniel > > > -- > View this message in context: > http://r.789695.n4.nabble.com/automatically-generate-the-output-name- > of-my-for-loops-tp3592160p3598027.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.
jiliguala
2011-Jun-19 13:58 UTC
[R] automatically generate the output name of my "for" loops
true, it did work using unlist() funciton, it can be found the minimum value. and for further step, if i want identify the minimum value with the list, which means the minimum value i have found is belonged to which list[[j]][[i]]. i used the codes like this ##which(list1==min(list1))## but it did not work. thanks for helping -- View this message in context: http://r.789695.n4.nabble.com/automatically-generate-the-output-name-of-my-for-loops-tp3592160p3609298.html Sent from the R help mailing list archive at Nabble.com.