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
use value in variable to be name of another variable
8 messages · Jim Lemon, David Winsemius, Rolf Turner +3 more
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.
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
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
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.
It appears that you are just trying to use the first row to create a column name for the rest of the column. If that is all you are doing something like this is quicker, but it uses the data frame.
set.seed(42) tTargTFS <- data.frame(matrix(replicate(100, paste0(sample(LETTERS, 6), collapse="")), 10), stringsAsFactors=FALSE) New <- tTargTFS[ -1, ] colnames(New)
[1] "X1" "X2" "X3" "X4" "X5" "X6" "X7" "X8" "X9" "X10"
colnames(New) <- tTargTFS[1, ] colnames(New)
[1] "XZGTOK" "RYSNXD" "JKNXPI" "XVHFQP" "BOZEMK" "MLBHTV" "OQGWZM" "CMXSLB" "GOUDTJ" [10] "SYMEXP"
str(New)
'data.frame': 9 obs. of 10 variables:
$ XZGTOK: chr "TDPQKX" "YGLVWC" "MOVDXT" "CMJUXR" ...
$ RYSNXD: chr "HUQFAC" "FLEQAH" "NAZDHX" "UOFCBG" ...
$ JKNXPI: chr "XYFQTM" "QXUNSC" "TPDBKQ" "TUEVGD" ...
$ XVHFQP: chr "XTDGEQ" "DZBXLC" "TSVLYJ" "ELXYFV" ...
$ BOZEMK: chr "EDLVHY" "HNASCL" "OPRCGT" "NDUEXS" ...
$ MLBHTV: chr "KDHXCU" "MCFVGN" "XYKJDF" "OXITPV" ...
$ OQGWZM: chr "QXJGBW" "TCRWEY" "BUNKIF" "PUCWDB" ...
$ CMXSLB: chr "MIQLWV" "LCWAJE" "CMPVHR" "HLDSOB" ...
$ GOUDTJ: chr "XGCBVK" "VKLEQG" "EADZLR" "CWNDTF" ...
$ SYMEXP: chr "RFIHTE" "RDCXUF" "XTYEBA" "HIRFLP" ...
-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352
-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Matthew
Sent: Monday, July 11, 2016 5:34 PM
To: Rolf Turner
Cc: r-help at r-project.org
Subject: Re: [R] use value in variable to be name of another variable
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
______________________________________________ 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.