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
Take a look in ?assign On Thu, Mar 31, 2011 at 5:42 PM, Noah Silverman <noah at smartmediacorp.com> wrote:> 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 > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Perfect. Thank You!!!! On Mar 31, 2011, at 1:47 PM, Mark Leeds wrote:> hi noah: assign(thing you paste, 123, envir=whatever) should work I think. > > > On Thu, Mar 31, 2011 at 4:42 PM, Noah Silverman <noah@smartmediacorp.com> wrote: > 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 > > ______________________________________________ > 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]]
The best thing to do is to understand how to use 'list' for this purpose. Much easier to handle the information. On Thu, Mar 31, 2011 at 4:42 PM, Noah Silverman <noah at smartmediacorp.com> wrote:> 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 > > ______________________________________________ > 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 Data Munger Guru What is the problem that you are trying to solve?