Brant Inman
2008-Feb-01 04:28 UTC
[R] Converting a character string into an object variable
R-helpers: Assume that I want to create a series of sequentially named R objects. For example, I might want to call these objects V1, V2, V3 ... V50. To do this, I thought of some sort of looping function like: input.value <- seq(1:50) * 3 for(i in 1:50){ paste("V", i, sep="") <- input.value[i] } Of course this loop will not work since the paste function returns a character string that cannot be a variable. How can I construct a loop to create sequential variables and assign a value to them in the same spirit of the loop above? Brant Inman Mayo Clinic [[alternative HTML version deleted]]
Benilton Carvalho
2008-Feb-01 04:35 UTC
[R] Converting a character string into an object variable
assign(paste("V", i, sep=""), input.value[i]) b On Jan 31, 2008, at 11:28 PM, Brant Inman wrote:> R-helpers: > > Assume that I want to create a series of sequentially named R > objects. For > example, I might want to call these objects V1, V2, V3 ... V50. To > do this, > I thought of some sort of looping function like: > > input.value <- seq(1:50) * 3 > for(i in 1:50){ > paste("V", i, sep="") <- input.value[i] > } > > > Of course this loop will not work since the paste function returns a > character string that cannot be a variable. How can I construct a > loop to > create sequential variables and assign a value to them in the same > spirit of > the loop above? > > Brant Inman > Mayo Clinic > > [[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.
Benilton Carvalho
2008-Feb-01 04:51 UTC
[R] Converting a character string into an object variable
You mean the list has elements called "V1" ... "V50"? assign(paste("output", i, sep=""), mylist[[paste("V", i, sep="")]]) b On Jan 31, 2008, at 11:44 PM, "Inman, Brant A., M.D." <Inman.Brant at mayo.edu > wrote:> Thanks B. > > Now what if V1, V2, V3 ...V50 are elements of a list and I wanted to > extract them? For example: > > output1 <- mylist[[V1]] > output2 <- mylist[[V2]] > ... > output50 <- mylist[[V50]] > > > Brant > -----Original Message----- > From: brant.inman+caf_=inman.brant=mayo.edu at gmail.com > [mailto:brant.inman+caf_=inman.brant=mayo.edu at gmail.com] On Behalf Of > Benilton Carvalho > Sent: Thursday, January 31, 2008 10:35 PM > To: Brant Inman > Cc: r-help at r-project.org > Subject: Re: [R] Converting a character string into an object variable > > assign(paste("V", i, sep=""), input.value[i]) > > b > > On Jan 31, 2008, at 11:28 PM, Brant Inman wrote: > >> R-helpers: >> >> Assume that I want to create a series of sequentially named R >> objects. For >> example, I might want to call these objects V1, V2, V3 ... V50. To >> do this, >> I thought of some sort of looping function like: >> >> input.value <- seq(1:50) * 3 >> for(i in 1:50){ >> paste("V", i, sep="") <- input.value[i] >> } >> >> >> Of course this loop will not work since the paste function returns a >> character string that cannot be a variable. How can I construct a >> loop to >> create sequential variables and assign a value to them in the same >> spirit of >> the loop above? >> >> Brant Inman >> Mayo Clinic >> >> [[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.
Bill.Venables at csiro.au
2008-Feb-01 04:52 UTC
[R] Converting a character string into an object variable
This is pretty standard. Here are two solutions, there are many more. input.value <- 3*(1:50) for(i in 1:50) assign(paste("V", i, sep = ""), input.value[i]) --- input.value <- 3*(1:50) for(i in 1:50) eval(substitute( V <- input.value[i], list(V = as.name(paste("V", i, sep = ""))))) --- The second is more obscure, but points to a more general way of doing things. I hope! Bill Venables. -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Brant Inman Sent: Friday, 1 February 2008 2:29 PM To: r-help at r-project.org Subject: [R] Converting a character string into an object variable R-helpers: Assume that I want to create a series of sequentially named R objects. For example, I might want to call these objects V1, V2, V3 ... V50. To do this, I thought of some sort of looping function like: input.value <- seq(1:50) * 3 for(i in 1:50){ paste("V", i, sep="") <- input.value[i] } Of course this loop will not work since the paste function returns a character string that cannot be a variable. How can I construct a loop to create sequential variables and assign a value to them in the same spirit of the loop above? Brant Inman Mayo Clinic [[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.