Hi, there: Suppose I want create variables with indexes in their names, e.g., X_1_1, X_1_2, X_1_3, ..., X_1_10, X_2_1, X_2_2, X_2_3, .. X_2_10,..., X_10_1, X_10_2, ... X_10_10. It looks like I need to use 2 indexes I and J so I is looped from 1 to 10, and J is looped from 1 to 10. But I don't know how to automatically produce X with these combination of indexes. Should I use paste function? Can someone help? Thanks. Yulei [[alternative HTML version deleted]]
Maybe I misunderstand your problem. For my understanding, it's quite simple.I hope it can help. id <- vector() for(i in 1:10){ for(j in 1:10){ id<-append(id,paste("X",i,j,sep="_")) } } Wenbo Mu On Tue, Jul 13, 2010 at 5:44 PM, He, Yulei <he at hcp.med.harvard.edu> wrote:> Hi, there: > > Suppose I want create variables with indexes in their names, e.g., X_1_1, X_1_2, X_1_3, ..., X_1_10, ?X_2_1, X_2_2, X_2_3, .. X_2_10,..., X_10_1, X_10_2, ... X_10_10. It looks like I need to use 2 indexes I and J so I is looped from 1 to 10, and J is looped from 1 to 10. But I don't know how to automatically produce X with these combination of indexes. Should I use paste function? Can someone help? > > Thanks. > > Yulei > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >
> Suppose I want create variables with indexes in their names, e.g., X_1_1, > X_1_2, X_1_3, ..., X_1_10, X_2_1, X_2_2, X_2_3, .. X_2_10,..., X_10_1, > X_10_2, ... X_10_10. It looks like I need to use 2 indexes I and J so I is > looped from 1 to 10, and J is looped from 1 to 10. But I don't know how to > automatically produce X with these combination of indexes. Should I use paste > function? Can someone help?In the long run, there's probably a better way to do what you want than creating all these variables, such as storing all your data in an appropriate structure such as a vector, matrix, list, or data.frame. All I can guess from your post that you want is to create a character vector of the strings you mention, in which case: outer(1:10, 1:10, function(x, y) paste("X", x, y,sep = "_")) will help. To see what I'm talking about in the first paragraph, see the difference between. X_1_1 <- 1 X_1_2 <- 2 X_2_1 <- 3 X_2_2 <- 4 vs. X <- matrix(1:4, ncol = 2) and then indexing using the usual style. X[1,2] X[1,] X[,1] etc...
Here is another way of going about it (presuming we are correct about what you are after): i=1:10 j=1:10 unlist(lapply(i, function(x) paste("X", x, j, sep="_"))) Tyler -- View this message in context: http://r.789695.n4.nabble.com/create-variables-with-indexes-tp2288119p2288171.html Sent from the R help mailing list archive at Nabble.com.
It looks like you want to use a matrix, then you can do proper indexing. Creating the names will just lead to unnecessary complexity and confusion. -- 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 He, Yulei > Sent: Tuesday, July 13, 2010 4:44 PM > To: 'r-help at lists.R-project.org' > Subject: [R] create variables with indexes > > Hi, there: > > Suppose I want create variables with indexes in their names, e.g., > X_1_1, X_1_2, X_1_3, ..., X_1_10, X_2_1, X_2_2, X_2_3, .. X_2_10,..., > X_10_1, X_10_2, ... X_10_10. It looks like I need to use 2 indexes I > and J so I is looped from 1 to 10, and J is looped from 1 to 10. But I > don't know how to automatically produce X with these combination of > indexes. Should I use paste function? Can someone help? > > Thanks. > > Yulei > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
In virtually every case where people think they need gobs of similarly-named variables, what they really need are vectors or data frames. I strongly recommend reading the Introduction to R document again to learn how to create them and access individual elements and subsets of elements.Data frames can be used to contain multiple "indexes" as you describe, and subset, logical vectors, and apply and the plyr library can be used to select and analyze parts of the data frame in a systematic manner. "He, Yulei" <he at hcp.med.harvard.edu> wrote:>Hi, there: > >Suppose I want create variables with indexes in their names, e.g., X_1_1, X_1_2, X_1_3, ..., X_1_10, X_2_1, X_2_2, X_2_3, .. X_2_10,..., X_10_1, X_10_2, ... X_10_10. It looks like I need to use 2 indexes I and J so I is looped from 1 to 10, and J is looped from 1 to 10. But I don't know how to automatically produce X with these combination of indexes. Should I use paste function? Can someone help? > >Thanks. > >Yulei > > > [[alternative HTML version deleted]] > >______________________________________________ >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.--------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity.
Hi r-help-bounces at r-project.org napsal dne 14.07.2010 00:44:02:> Hi, there: > > Suppose I want create variables with indexes in their names, e.g.,X_1_1,> X_1_2, X_1_3, ..., X_1_10, X_2_1, X_2_2, X_2_3, .. X_2_10,..., X_10_1, > X_10_2, ... X_10_10. It looks like I need to use 2 indexes I and J so Iis> looped from 1 to 10, and J is looped from 1 to 10. But I don't know howto> automatically produce X with these combination of indexes. Should I usepaste> function? Can someone help?I would make a character vector, which I could use in further naming purpose. paste(paste("x", rep(1:10, 10), sep="_"), rep(1:10, each=10), sep="_") But why not to use lists? Petr> > Thanks. > > Yulei > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.