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
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.
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.
David Winsemius
2016-Jul-11 22:24 UTC
[R] use value in variable to be name of another variable
> 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