Hi Jim, Wow ! And it does exactly what I was looking for. Thank you very much. That assign function is pretty nice. I should become more familiar with it. Matthew On 7/11/2016 5:59 PM, Jim Lemon wrote:> Hi Matthew, > This question is a bit mysterious as we don't know what the object > "chr" is. However, have a look at this and see if it is close to what > you want to do. > > # set up a little matrix of character values > tTargTFS<-matrix(paste("A",rep(1:4,each=4),"B",rep(1:4,4),sep=""),ncol=4) > # try the assignment on the first row and column > assign(tTargTFS[1,1],tTargTFS[-1,1]) > # see what it looks like - okay > A1B1 > # run the assignment over the matrix > for(i in 1:4) assign(tTargTFS[1,i],tTargTFS[-1,i]) > # see what the variables look like > A1B1 > A2B1 > A3B1 > A4B1 > > It does what I would expect. > > Jim > > > On Tue, Jul 12, 2016 at 6:01 AM, Matthew > <mccormack at molbio.mgh.harvard.edu> wrote: >> I want to get a value that has been assigned to a variable, and then use >> that value to be the name of a variable. >> >> For example, >> >> tTargTFS[1,1] >> # returns: >> V1 >> "AT1G01010" >> >> Now, I want to make AT1G01010 the name of a variable: >> AT1G01010 <- tTargTFS[-1,1] >> >> Then, go to the next tTargTFS[1,2]. Which produces >> V1 >> "AT1G01030" >> And then, >> AT1G01030 <- tTargTFS[-1,2] >> >> I want to do this up to tTargTFS[1, 2666], so I want to do this in a script >> and not manually. >> tTargTFS is a list of 2: chr [1:265, 1:2666], but I also have the data in a >> data frame of 265 observations of 2666 variables, if this data structure >> makes things easier. >> >> My initial attempts are not working. Starting with a test data structure >> that is a little simpler I have tried: >> for (i in 1:4) >> { ATG <- tTargTFS[1, i] >> assign(cat(ATG), tTargTFS[-1, i]) } >> >> Matthew >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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.
Rolf Turner
2016-Jul-11 22:31 UTC
[R] use value in variable to be name of another variable
On 12/07/16 10:13, Matthew wrote:> Hi Jim, > > Wow ! And it does exactly what I was looking for. Thank you very much. > > That assign function is pretty nice. I should become more familiar with it.Indeed you should, and assign() is indeed nice and useful and handy. But it should be used with care and circumspection. It *alters the global environment* which is fraught with peril. Generally speaking most things that can be done with assign() (and its companion function get()) are better and more safely done using lists and functions and other "natural" R-ish constructs. Resist the temptation to turn R into a macro language. cheers, Rolf Turner -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276
Hi Rolf, Thanks for the warning. I think because my initial efforts used the assign function, that Jim provided his solution using it. Any suggestions for how it could be done without assign() ? Matthew On 7/11/2016 6:31 PM, Rolf Turner wrote:> On 12/07/16 10:13, Matthew wrote: >> Hi Jim, >> >> Wow ! And it does exactly what I was looking for. Thank you very >> much. >> >> That assign function is pretty nice. I should become more familiar >> with it. > > Indeed you should, and assign() is indeed nice and useful and handy. > But it should be used with care and circumspection. It *alters the > global environment* which is fraught with peril. Generally speaking > most things that can be done with assign() (and its companion function > get()) are better and more safely done using lists and functions and > other "natural" R-ish constructs. Resist the temptation to turn R into > a macro language. > > cheers, > > Rolf Turner >
William Dunlap
2016-Jul-11 22:43 UTC
[R] use value in variable to be name of another variable
I find that instead of using assign() and get(), it is more convenient to make an environment in which to store a related set of variables and then use env[[varName]] instead of get(varName) or assign(varName) to get and set variables. The advantages are * the same syntax works for setting and getting, unlike assign() and get() * nested replacements work * you don't accidently overwrite things in the current environment You can use the same syntax with a list instead of an environment. E.g., geneNames <- c("AT1", "AT2", "PQ1") envAction <- new.env(parent=emptyenv()) envAction[[ geneNames[2] ]] <- paste("Action for", geneNames[[2]]) names(envAction) envAction[[ geneNames[2] ]] envAction[[ geneNames[2] ]] [2] <- "another action" # nested replacement envAction[[ geneNames[2] ]] Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Jul 11, 2016 at 3:13 PM, Matthew <mccormack at molbio.mgh.harvard.edu> wrote:> Hi Jim, > > Wow ! And it does exactly what I was looking for. Thank you very much. > > That assign function is pretty nice. I should become more familiar with it. > > Matthew > > > On 7/11/2016 5:59 PM, Jim Lemon wrote: > >> Hi Matthew, >> This question is a bit mysterious as we don't know what the object >> "chr" is. However, have a look at this and see if it is close to what >> you want to do. >> >> # set up a little matrix of character values >> tTargTFS<-matrix(paste("A",rep(1:4,each=4),"B",rep(1:4,4),sep=""),ncol=4) >> # try the assignment on the first row and column >> assign(tTargTFS[1,1],tTargTFS[-1,1]) >> # see what it looks like - okay >> A1B1 >> # run the assignment over the matrix >> for(i in 1:4) assign(tTargTFS[1,i],tTargTFS[-1,i]) >> # see what the variables look like >> A1B1 >> A2B1 >> A3B1 >> A4B1 >> >> It does what I would expect. >> >> Jim >> >> >> On Tue, Jul 12, 2016 at 6:01 AM, Matthew >> <mccormack at molbio.mgh.harvard.edu> wrote: >> >>> I want to get a value that has been assigned to a variable, and then use >>> that value to be the name of a variable. >>> >>> For example, >>> >>> tTargTFS[1,1] >>> # returns: >>> V1 >>> "AT1G01010" >>> >>> Now, I want to make AT1G01010 the name of a variable: >>> AT1G01010 <- tTargTFS[-1,1] >>> >>> Then, go to the next tTargTFS[1,2]. Which produces >>> V1 >>> "AT1G01030" >>> And then, >>> AT1G01030 <- tTargTFS[-1,2] >>> >>> I want to do this up to tTargTFS[1, 2666], so I want to do this in a >>> script >>> and not manually. >>> tTargTFS is a list of 2: chr [1:265, 1:2666], but I also have the data >>> in a >>> data frame of 265 observations of 2666 variables, if this data structure >>> makes things easier. >>> >>> My initial attempts are not working. Starting with a test data structure >>> that is a little simpler I have tried: >>> for (i in 1:4) >>> { ATG <- tTargTFS[1, i] >>> assign(cat(ATG), tTargTFS[-1, i]) } >>> >>> Matthew >>> >>> ______________________________________________ >>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >>> 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 -- To UNSUBSCRIBE and more, see > 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]]