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

David Winsemius dwinsemius at comcast.net
Tue Jul 12 00:24:27 CEST 2016


> On Jul 11, 2016, at 1:01 PM, 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]) }

Your efforts will come to naught (or more prezactly...  NULL) when you use `cat` as a value. You are essentially doing the R equivalent of answering the question about the sound of one hand clapping.

-- 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list