Dear folks-- Suppose I have an expression that evaluates to a string, and that that string, were it not a character vector, would be a symbol. I would like a function, call it doppel(), that will take that expression as an argument and produce something that functions exactly like the symbol would have if I typed it in the place of the function of the expression. It should go as far along the path to evaluation as the symbol would have, and then stop, and be available for subsequent manipulation. For example, if aa <- 3.1416 bb <- function(x) {x^2} r <- 2 xx <- c("aa", "bb") out <- doppel(xx[1])*doppel(xx[2])(r) Then out should be 13.3664 Or similarly, after doppel(paste("a", "a", sep='')) <- 3 aa typing aa should return 3. Is there such a function? Can there be? I thought as.symbol would do this, but it does not.> as.symbol (xx[1])*as.symbol (xx[2])(r)Error: attempt to apply non-function Looking forward to hearing from y'all. --andrewH -- View this message in context: http://r.789695.n4.nabble.com/Can-you-turn-a-string-into-a-working-symbol-tp4648343.html Sent from the R help mailing list archive at Nabble.com.
Is this what you want (the answer you "wanted" is not correct):> aa <- 3.1416 > bb <- function(x) {x^2} > r <- 2 > xx <- c("aa", "bb") > > doppel <- function(x) get(x) > > out <- doppel(xx[1])*doppel(xx[2])(r) > > out[1] 12.5664>On Sat, Nov 3, 2012 at 5:31 PM, andrewH <ahoerner at rprogress.org> wrote:> Dear folks-- > > Suppose I have an expression that evaluates to a string, and that that > string, were it not a character vector, would be a symbol. I would like a > function, call it doppel(), that will take that expression as an argument > and produce something that functions exactly like the symbol would have if I > typed it in the place of the function of the expression. It should go as > far along the path to evaluation as the symbol would have, and then stop, > and be available for subsequent manipulation. For example, if > > aa <- 3.1416 > bb <- function(x) {x^2} > r <- 2 > xx <- c("aa", "bb") > > out <- doppel(xx[1])*doppel(xx[2])(r) > > Then out should be 13.3664 > > Or similarly, after > doppel(paste("a", "a", sep='')) <- 3 > aa > > typing aa should return 3. > > Is there such a function? Can there be? > > I thought as.symbol would do this, but it does not. >> as.symbol (xx[1])*as.symbol (xx[2])(r) > Error: attempt to apply non-function > > Looking forward to hearing from y'all. --andrewH > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Can-you-turn-a-string-into-a-working-symbol-tp4648343.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
for the second part use 'assign'> assign(paste0('a', 'a'), 3) > aa[1] 3>On Sat, Nov 3, 2012 at 5:31 PM, andrewH <ahoerner at rprogress.org> wrote:> Dear folks-- > > Suppose I have an expression that evaluates to a string, and that that > string, were it not a character vector, would be a symbol. I would like a > function, call it doppel(), that will take that expression as an argument > and produce something that functions exactly like the symbol would have if I > typed it in the place of the function of the expression. It should go as > far along the path to evaluation as the symbol would have, and then stop, > and be available for subsequent manipulation. For example, if > > aa <- 3.1416 > bb <- function(x) {x^2} > r <- 2 > xx <- c("aa", "bb") > > out <- doppel(xx[1])*doppel(xx[2])(r) > > Then out should be 13.3664 > > Or similarly, after > doppel(paste("a", "a", sep='')) <- 3 > aa > > typing aa should return 3. > > Is there such a function? Can there be? > > I thought as.symbol would do this, but it does not. >> as.symbol (xx[1])*as.symbol (xx[2])(r) > Error: attempt to apply non-function > > Looking forward to hearing from y'all. --andrewH > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Can-you-turn-a-string-into-a-working-symbol-tp4648343.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
Ah! Excellent! That will be most useful. And sorry about the typo. I found another function in a different discussion that also seems to work, at least in most cases I have tried. I do not at all understand the difference between the two. doppel <- function(x) {eval(parse(text=x)) However, neither one seems to work on the left hand side of a "<-", a "<<-", or an "=". Again, my thanks.--andrewH -- View this message in context: http://r.789695.n4.nabble.com/Can-you-turn-a-string-into-a-working-symbol-tp4648343p4648365.html Sent from the R help mailing list archive at Nabble.com.
Yes, the assign command goes a little way toward what what I was hoping for. But it requires a different syntax, and it does not in general let you use quoted expressions that you could use with other assignment operators. For instance,> DD <- 1:3 > assign("DD[2]", 5) > DD[1] 1 2 3 So I am still looking for a function that produces an output that is fully equivalent to the string without quotation marks. Or for a definite statement that no such function can exist. Thanks so much for your attention to this problem. andrewH -- View this message in context: http://r.789695.n4.nabble.com/Can-you-turn-a-string-into-a-working-symbol-tp4648343p4648366.html Sent from the R help mailing list archive at Nabble.com.
HI, get("DD[2]") #[1] 5 If you wanted to change the one without quotes: ?new1<-function(x,y) {eval.parent(substitute(x<-y))} ?new1(DD[2],7) ?DD[2] #[1] 7 DD #[1] 1 7 3 A.K. ----- Original Message ----- From: andrewH <ahoerner at rprogress.org> To: r-help at r-project.org Cc: Sent: Saturday, November 3, 2012 11:10 PM Subject: Re: [R] Can you turn a string into a (working) symbol? Yes, the assign command goes a little way toward what what I was hoping for. But it requires a different syntax, and it does not in general let you use quoted expressions that you could? use with other assignment operators. For instance,> DD <- 1:3 > assign("DD[2]", 5) > DD[1] 1 2 3 So I am still looking for a function that produces an output that is fully equivalent to the string without quotation marks.? Or for a definite statement that no such function can exist. Thanks so much for your attention to this problem. andrewH -- View this message in context: http://r.789695.n4.nabble.com/Can-you-turn-a-string-into-a-working-symbol-tp4648343p4648366.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list 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.
Others have shown you ways to do what you asked, and what you asked happens to also be FAQ 7.22 (but with terms different enough that that FAQ is not obvious). However the solutions that you are tending towards are running into the problems addressed in fortune(106) and fortune(236), the solutions are getting complex, prone to error or worse problems. Can you tell us more about what you are trying to accomplish? There may be a much simpler solution than the approach that you are trying. For example it may greatly simplify things if you store some of your objects in a list rather than the global environment and worked with them there:> doppel <- list() > doppel$aa <- 3.1416 > doppel$bb <- function(x) {x^2} > r <- 2 > xx <- c("aa", "bb") > > out <- doppel[[ xx[1] ]] * doppel[[ xx[2] ]](r) > out[1] 12.5664> > doppel[[ paste( 'a','a', sep='' ) ]] <- 3 > > doppel$aa[1] 3> with(doppel, aa)[1] 3 On Sat, Nov 3, 2012 at 3:31 PM, andrewH <ahoerner@rprogress.org> wrote:> Dear folks-- > > Suppose I have an expression that evaluates to a string, and that that > string, were it not a character vector, would be a symbol. I would like a > function, call it doppel(), that will take that expression as an argument > and produce something that functions exactly like the symbol would have if > I > typed it in the place of the function of the expression. It should go as > far along the path to evaluation as the symbol would have, and then stop, > and be available for subsequent manipulation. For example, if > > aa <- 3.1416 > bb <- function(x) {x^2} > r <- 2 > xx <- c("aa", "bb") > > out <- doppel(xx[1])*doppel(xx[2])(r) > > Then out should be 13.3664 > > Or similarly, after > doppel(paste("a", "a", sep='')) <- 3 > aa > > typing aa should return 3. > > Is there such a function? Can there be? > > I thought as.symbol would do this, but it does not. > > as.symbol (xx[1])*as.symbol (xx[2])(r) > Error: attempt to apply non-function > > Looking forward to hearing from y'all. --andrewH > > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Can-you-turn-a-string-into-a-working-symbol-tp4648343.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > 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@gmail.com [[alternative HTML version deleted]]
Dear Greg? You mean FAQ 7.21, not 7.22, correct? Though 7.12 also seems relevant. Though I would say I was asking about turning a string into an expression rather than a variable. At any rate, thanks for the pointer. I sure I would benefit from rereading the FAQ on a monthly basis, until I actually know most of what is in it. As to your question about my question, I?ve wanted to do this exact thing several times in different contexts. However, you are quite correct that I am struggling with this problem in a particular context. I have of a large, multi-dimensional object containing count data. Currently this object is implemented as a 26 dimensional (and growing) array with two to thirteen dimnames per dimension, though I am thinking of switching it to a data frame with dimensions as factors and dimname-equivilent factor levels. I need to take a lot of complicated partitions of this object, mainly, though not always, summing to the entire object. Most of the partitions are subsets of -- ? OK, now I have to digress to address a terminological uncertainty. Think of a 4X4X4 cube. It has three dimensions, and each dimension has four of what? I?m going to call them levels right now, though I don?t think that is right -- it would be confusing if there were factors in the picture. Also, the dimnames do not name the dimensions, but the thing I am calling levels, which is also confusing. -- Anyway, most of the partitions consist of two to four dimensions out of 24, but sometimes with some levels omitted or summed, and occasionally the partitions that are much more complicated (to deal with censored data, mainly). I have to use each partition multiple times, doing a very different thing each time (and then repeat the whole set many times) The next 4 paragraphs describe what I am actually doing with the partitions, but you can skip over them and cut to the chase if you are not so interested. I am summing over the dimensions in each partition, dividing a table of ?forcing totals? for that partition by those sums (element by element), and then taking the resulting ratios and multiplying each of the terms in the original, non-summed object by the corresponding ratio. This is easiest to understand by analogy to the two-dimensional case. You take the row sums and divide them element by element by a vector of pre-determined row ?forcing totals,? to get a vector of forcing ratios. Then you multiply each row by the corresponding forcing ratio, so that the row sum will then match the forcing total. Then you do the same thing with the columns. Repeat, alternating row and columns, to convergence. Each column has a corresponding column forcing total, and each row has a corresponding row forcing total. The elements of the matrix have two partitions that we use, one into rows, and the other into columns. This is sometimes called RAS balancing, or biproportional matrix adjustment. It is an algorithm that is used a lot to update big matrices in national income accounting and input-output analysis. What I am doing is the same, but I have forcing totals in two to four dimensional tables instead of a one dimensional vectors. Each partition divides the array into groups of elements that I want to sum to my forcing totals. Again, you go around in a circle, doing forcing with each of the (currently 18) tables, to convergence. On count data it should always converge. The thing is, I need to keep track of all these partitions, and then multiply the forcing totals by the exact same elements of the array as I previously summed. I got up to five dimensions, coding by hand, and then realized that 1: the amount of work in going from, e.g., 19 dimensions to 20 was going to very great, and 2. the likelihood that I would get all the nesting and partition-matching right was vanishingly small. So I am looking for a way to encode the partitions that I use, that would allow me to use the same encoding to represent both the subsets of the array to sum over, crunching the array down to a set of totals corresponding to my forcing totals, and also defining the subsets of the array that should be multiplied by each forcing ratio. And I thought, maybe I could do it with strings of indexing commands, one per table of forcing totals. But this will only work If I can sum the array over the subdivisions that the partition defines, multiply all the elements in partition subdivisions by the corresponding constants, and then assign the results back to the array, or to a new array. Hence my question. I?m afraid that this explanation is too long for people to read, but hope springs eternal. I?d be remarkably pleased and eternally grateful if I got a solution to the problem of keeping track of partitions that can be used in the three ways described in the previous paragraph, even if it has nothing to do with executing strings. Warmest regards, andrewH -- View this message in context: http://r.789695.n4.nabble.com/Can-you-turn-a-string-into-a-working-symbol-tp4648343p4651073.html Sent from the R help mailing list archive at Nabble.com.
Dear folks-- Suppose I have an expression that evaluates to a string, and that that string, were it not a character vector, would be a symbol. I would like a function, call it doppel(), that will take that expression as an argument and produce something that functions exactly like the symbol would have if I typed it in the place of the function of the expression. It should go as far along the path to evaluation as the symbol would have, and then stop, and be available for subsequent manipulation. For example, if aa <- 3.1416 bb <- function(x) {x^2} r <- 2 xx <- c("aa", "bb") out <- doppel(xx[1])*doppel(xx[2])(r) Then out should be 13.3664 Or similarly, after doppel(paste("a", "a", sep='')) <- 3 aa typing aa should return 3. Is there such a function? Can there be? I thought as.symbol would do this, but it does not.> as.symbol (xx[1])*as.symbol (xx[2])(r)Error: attempt to apply non-function ----- -- View this message in context: http://r.789695.n4.nabble.com/Can-you-turn-a-string-into-a-working-symbol-tp4651634.html Sent from the R help mailing list archive at Nabble.com.