Skip to content

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:
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
#
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:

  
    
#
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:

  
    
#
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)) )