[R] use value in variable to be name of another variable

William Dunlap wdunlap at tibco.com
Tue Jul 12 00:43:09 CEST 2016


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]]



More information about the R-help mailing list