Jun Shen
2015-Jul-07 20:53 UTC
[R] How to assign value to a variable dynamically constructed
Dear list, Let's say we have a variable (id), whose name is dynamically constructed. This variable represents a vector or data frame with many elements. Now I want to specifically assign a value to one of the elements. I couldn't get it right. test <- 'id' # "id" is dynamically constructed through paste() id <- 1:4 # I can get the element by doing get(test)[2] # Now I want to assign a value to the second element of this dynamical variable. get(test)[2] <- 5 # doesn't work. Thanks a lot. Jun Shen [[alternative HTML version deleted]]
Bert Gunter
2015-Jul-07 21:23 UTC
[R] How to assign value to a variable dynamically constructed
Try reading the Help files before posting here. That's what they're for. ?get provides the answer in a note in the Help page. Cheers, Bert Bert Gunter "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." -- Clifford Stoll On Tue, Jul 7, 2015 at 1:53 PM, Jun Shen <jun.shen.ut at gmail.com> wrote:> Dear list, > > Let's say we have a variable (id), whose name is dynamically constructed. > This variable represents a vector or data frame with many elements. Now I > want to specifically assign a value to one of the elements. I couldn't get > it right. > > test <- 'id' # "id" is dynamically constructed through paste() > > id <- 1:4 > > # I can get the element by doing > > get(test)[2] > > # Now I want to assign a value to the second element of this dynamical > variable. > > get(test)[2] <- 5 # doesn't work. > > Thanks a lot. > > Jun Shen > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
Greg Snow
2015-Jul-08 17:06 UTC
[R] How to assign value to a variable dynamically constructed
This is FAQ 7.21. The most important part of the answer in FAQ 7.21 is the last section where it states that it is often easier to use a list rather than messing around with trying to dynamically name global variables. If you tell us what you are trying to accomplish then we may have better advice. The route you are headed down now usually leads to inefficient code and hard to find bugs. On Tue, Jul 7, 2015 at 2:53 PM, Jun Shen <jun.shen.ut at gmail.com> wrote:> Dear list, > > Let's say we have a variable (id), whose name is dynamically constructed. > This variable represents a vector or data frame with many elements. Now I > want to specifically assign a value to one of the elements. I couldn't get > it right. > > test <- 'id' # "id" is dynamically constructed through paste() > > id <- 1:4 > > # I can get the element by doing > > get(test)[2] > > # Now I want to assign a value to the second element of this dynamical > variable. > > get(test)[2] <- 5 # doesn't work. > > Thanks a lot. > > Jun Shen > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com
William Dunlap
2015-Jul-08 17:28 UTC
[R] How to assign value to a variable dynamically constructed
You can use an environment instead of a list using the same [[ syntax. It is like 'get0(..., inherit=FALSE)' on the left side of the <- and like 'assign(...)' on the right side. E.g., myData <- new.env() varName <- "v1" myData[[varName]] <- 1:10 myData[[varName]][4] <- myData[[varName]][4] * 100 myData[[varName]] # [1] 1 2 3 400 5 6 7 8 9 10 names(myData) # [1] "v1" (Before R-3.2.0 or so, you had to use objects(myData,all=TRUE) if myData was an environment and names(myData) if it was a list. Now names() works for environments.) It is better to use a dedicated environment (or list) for each set of related variables so that name collisions do not cause problems. Bill Dunlap TIBCO Software wdunlap tibco.com On Wed, Jul 8, 2015 at 10:06 AM, Greg Snow <538280 at gmail.com> wrote:> This is FAQ 7.21. > > The most important part of the answer in FAQ 7.21 is the last section > where it states that it is often easier to use a list rather than > messing around with trying to dynamically name global variables. > > If you tell us what you are trying to accomplish then we may have > better advice. The route you are headed down now usually leads to > inefficient code and hard to find bugs. > > On Tue, Jul 7, 2015 at 2:53 PM, Jun Shen <jun.shen.ut at gmail.com> wrote: > > Dear list, > > > > Let's say we have a variable (id), whose name is dynamically constructed. > > This variable represents a vector or data frame with many elements. Now I > > want to specifically assign a value to one of the elements. I couldn't > get > > it right. > > > > test <- 'id' # "id" is dynamically constructed through paste() > > > > id <- 1:4 > > > > # I can get the element by doing > > > > get(test)[2] > > > > # Now I want to assign a value to the second element of this dynamical > > variable. > > > > get(test)[2] <- 5 # doesn't work. > > > > Thanks a lot. > > > > Jun Shen > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > 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. > > > > -- > Gregory (Greg) L. Snow Ph.D. > 538280 at gmail.com > > ______________________________________________ > 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]]