Hi: I need to create many variables at one time,how to do this in R? for eg ,X1,X2.......X100? Thanks~ [[alternative HTML version deleted]]
On Wed, Mar 4, 2009 at 2:34 PM, Manli Yan <manliyanrhelp at gmail.com> wrote:> ?Hi: > ?I need to create many variables at one time,how to do this in R? > ?for eg ,X1,X2.......X100?It depends what you want. If you want 100 random normal variables of length 10, stored in a data.frame with names V1, V2, ..., V100 try dat <- as.data.frame(replicate(100, rnorm(10))) hth, Kingsford Jones> > ?Thanks~ > > ? ? ? ?[[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. >
Hi Manli, you may consider structuring your data in some appropriate form like data.frame or list. Its often not the best way holding information separated in many variables. But if you *really* want to create 100 separate variables, something like for (i in 1:100) assign(paste("X",i,sep=""), some_values) will do the job. Manli Yan schrieb:> Hi: > I need to create many variables at one time,how to do this in R? > for eg ,X1,X2.......X100? > > Thanks~ > > [[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. >-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/42803-8243 F ++49/40/42803-7790
Dear Manli, Do you mean the names of the variables? If so, something like this should work: paste('X',1:100,sep="") HTH, Jorge On Wed, Mar 4, 2009 at 4:34 PM, Manli Yan <manliyanrhelp@gmail.com> wrote:> Hi: > I need to create many variables at one time,how to do this in R? > for eg ,X1,X2.......X100? > > Thanks~ > > [[alternative HTML version deleted]] > > ______________________________________________ > 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]]
Have you considered using a 'list'? much easier to manage than a lot of individual objects. mylist <- lapply(1:100, runif) On Wed, Mar 4, 2009 at 4:34 PM, Manli Yan <manliyanrhelp at gmail.com> wrote:> ?Hi: > ?I need to create many variables at one time,how to do this in R? > ?for eg ,X1,X2.......X100? > > ?Thanks~ > > ? ? ? ?[[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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
You can also change the column names to something else en mass: colnames(dat) <- paste("X",1:100,sep="") I next tried constructing the X<n> names inside data.frame, but failed using the paste function. The help page for data.frame has a paragraph that begins "How the names of the data frame are created is complex..." At least the following will result in names of the form X.1, X.2 ... dat <- data.frame( X = replicate(100, rnorm(10)) ) -- David Winsemius On Mar 4, 2009, at 4:44 PM, Kingsford Jones wrote:> On Wed, Mar 4, 2009 at 2:34 PM, Manli Yan <manliyanrhelp at gmail.com> > wrote: >> Hi: >> I need to create many variables at one time,how to do this in R? >> for eg ,X1,X2.......X100? > > It depends what you want. If you want 100 random normal variables of > length 10, stored in a data.frame with names V1, V2, ..., V100 try > > dat <- as.data.frame(replicate(100, rnorm(10))) > > > hth, > Kingsford Jones > >> >> Thanks~ >> >> [[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. >> > > ______________________________________________ > 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.