[R] Create Variable names dynamically

Noah Silverman noah at smartmediacorp.com
Thu Mar 31 22:42:54 CEST 2011


Hi,

I want to create variable names from within my code, but can't find any documentation for this.

An example is probably the best way to illustrate. I am reading data in from a file, doing a bunch of stuff, and want to generate variables with my output.  (I could make a "list of lists" and name all the elements, but I really want separate variables.)


#################
#This is just a dummy example, please excuse any shortcuts...

data <- read.table("file", ....)
animals <- (data[,animal])  
animals
> "cat", "dog", "horse"  # Not known what these are before I read the data file

# do a bunch of stuff

mean_cat <- abc
var_cat <- dfd
mean_dog <- 123
var_dog <- 453
etc..
##############

I thought of trying to use the paste() function to create the variable name, but that doesn't work:
for( animal in animals){
	paste("mean", animal "_") <- 123
}

Any ideas???

Thanks


--
Noah Silverman



More information about the R-help mailing list