An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090304/d5e4bbc0/attachment-0002.pl>
how to create many variables at one time?
6 messages · Manli Yan, Kingsford Jones, Eik Vettorazzi +3 more
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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090304/3d520b10/attachment-0002.pl>
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.