[R] replacing characters in matrix. substitute, delayedAssign, huh?
A user question today has me stumped. Can you advise me, please? User wants a matrix that has some numbers, some variables, possibly even some function names. So that has to be a character matrix. Consider: BM - matrix(0.1, 5, 5) Use data.entry(BM) or similar to set some to more abstract values. BM[3,1] - a BM[4,2] - b BM[5,2] - b BM[5,3] - d BM var1 var2 var3 var4 var5 [1,] 0.1 0.1 0.1 0.1 0.1 [2,] 0.1 0.1 0.1 0.1 0.1 [3,] a 0.1 0.1 0.1 0.1 [4,] 0.1 b 0.1 0.1 0.1 [5,] 0.1 b d 0.1 0.1 Later on, user code will set values, e.g., a - rnorm(1) b - 17 d - 4 Now, push those into BM, convert whole thing to numeric newBM - apply(BM, c(1,2), as.numeric) and use newBM for some big calculation. Then re-set new values for a, b, d, do the same over again. I've been trying lots of variations on parse, substitute, and eval. The most interesting function I learned about this morning was delayedAssign. If I had only to work with one scalar, it does what I want delayedAssign(a, whatA) whatA - 91 a [1] 91 I can't see how to make that work in the matrix context, though. Got ideas? pj sessionInfo() R version 2.14.1 (2011-12-22) Platform: x86_64-pc-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=C LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] tools_2.14.1 -- Paul E. Johnson Professor, Political Science 1541 Lilac Lane, Room 504 University of Kansas __ 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.
Re: [R] replacing characters in matrix. substitute, delayedAssign, huh?
Are you sure this isn't a dataframe? Some minor rethinking of the structure might get it there. Rich On Mon, Jan 30, 2012 at 1:26 PM, Paul Johnson pauljoh...@gmail.com wrote: A user question today has me stumped. Can you advise me, please? User wants a matrix that has some numbers, some variables, possibly even some function names. So that has to be a character matrix. Consider: BM - matrix(0.1, 5, 5) Use data.entry(BM) or similar to set some to more abstract values. BM[3,1] - a BM[4,2] - b BM[5,2] - b BM[5,3] - d BM var1 var2 var3 var4 var5 [1,] 0.1 0.1 0.1 0.1 0.1 [2,] 0.1 0.1 0.1 0.1 0.1 [3,] a 0.1 0.1 0.1 0.1 [4,] 0.1 b 0.1 0.1 0.1 [5,] 0.1 b d 0.1 0.1 Later on, user code will set values, e.g., a - rnorm(1) b - 17 d - 4 Now, push those into BM, convert whole thing to numeric newBM - apply(BM, c(1,2), as.numeric) and use newBM for some big calculation. Then re-set new values for a, b, d, do the same over again. I've been trying lots of variations on parse, substitute, and eval. The most interesting function I learned about this morning was delayedAssign. If I had only to work with one scalar, it does what I want delayedAssign(a, whatA) whatA - 91 a [1] 91 I can't see how to make that work in the matrix context, though. Got ideas? pj sessionInfo() R version 2.14.1 (2011-12-22) Platform: x86_64-pc-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=C LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] tools_2.14.1 -- Paul E. Johnson Professor, Political Science 1541 Lilac Lane, Room 504 University of Kansas __ 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.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ 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.
Re: [R] replacing characters in matrix. substitute, delayedAssign, huh?
The quick solution: parseAndEval - function(x, ...) eval(parse(text=x)) apply(BM, MARGIN=c(1,2), FUN=parseAndEval) My $.02 /Henrik On Mon, Jan 30, 2012 at 10:26 AM, Paul Johnson pauljoh...@gmail.com wrote: A user question today has me stumped. Can you advise me, please? User wants a matrix that has some numbers, some variables, possibly even some function names. So that has to be a character matrix. Consider: BM - matrix(0.1, 5, 5) Use data.entry(BM) or similar to set some to more abstract values. BM[3,1] - a BM[4,2] - b BM[5,2] - b BM[5,3] - d BM var1 var2 var3 var4 var5 [1,] 0.1 0.1 0.1 0.1 0.1 [2,] 0.1 0.1 0.1 0.1 0.1 [3,] a 0.1 0.1 0.1 0.1 [4,] 0.1 b 0.1 0.1 0.1 [5,] 0.1 b d 0.1 0.1 Later on, user code will set values, e.g., a - rnorm(1) b - 17 d - 4 Now, push those into BM, convert whole thing to numeric newBM - apply(BM, c(1,2), as.numeric) and use newBM for some big calculation. Then re-set new values for a, b, d, do the same over again. I've been trying lots of variations on parse, substitute, and eval. The most interesting function I learned about this morning was delayedAssign. If I had only to work with one scalar, it does what I want delayedAssign(a, whatA) whatA - 91 a [1] 91 I can't see how to make that work in the matrix context, though. Got ideas? pj sessionInfo() R version 2.14.1 (2011-12-22) Platform: x86_64-pc-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=C LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] tools_2.14.1 -- Paul E. Johnson Professor, Political Science 1541 Lilac Lane, Room 504 University of Kansas __ 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. __ 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.
Re: [R] replacing characters in matrix. substitute, delayedAssign, huh?
On Mon, Jan 30, 2012 at 1:26 PM, Paul Johnson pauljoh...@gmail.com wrote: A user question today has me stumped. Can you advise me, please? User wants a matrix that has some numbers, some variables, possibly even some function names. So that has to be a character matrix. Consider: BM - matrix(0.1, 5, 5) Use data.entry(BM) or similar to set some to more abstract values. BM[3,1] - a BM[4,2] - b BM[5,2] - b BM[5,3] - d BM var1 var2 var3 var4 var5 [1,] 0.1 0.1 0.1 0.1 0.1 [2,] 0.1 0.1 0.1 0.1 0.1 [3,] a 0.1 0.1 0.1 0.1 [4,] 0.1 b 0.1 0.1 0.1 [5,] 0.1 b d 0.1 0.1 Later on, user code will set values, e.g., a - rnorm(1) b - 17 d - 4 Now, push those into BM, convert whole thing to numeric newBM - apply(BM, c(1,2), as.numeric) and use newBM for some big calculation. Then re-set new values for a, b, d, do the same over again. I've been trying lots of variations on parse, substitute, and eval. The most interesting function I learned about this morning was delayedAssign. If I had only to work with one scalar, it does what I want delayedAssign(a, whatA) whatA - 91 a [1] 91 I can't see how to make that work in the matrix context, though. You can do this: m - list(a, 1L, 2.5, function(x)x^2) dim(m) - c(2, 2) m [,1] [,2] [1,] a 2.5 [2,] 1? # Run the function in 2,2 passing it argument in 1,2 m[[2,2]]( m[[1, 2]] ) [1] 6.25 -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.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.
Re: [R] replacing characters in matrix. substitute, delayedAssign, huh?
On 30/01/2012 1:26 PM, Paul Johnson wrote: A user question today has me stumped. Can you advise me, please? User wants a matrix that has some numbers, some variables, possibly even some function names. So that has to be a character matrix. It might make more sense for it to be a list-mode matrix. Lists are vectors, and if they have dimension, they are matrices, but the entries need not be the same types. Consider: BM- matrix(0.1, 5, 5) Use data.entry(BM) or similar to set some to more abstract values. BM[3,1]- a BM[4,2]- b BM[5,2]- b BM[5,3]- d BM var1 var2 var3 var4 var5 [1,] 0.1 0.1 0.1 0.1 0.1 [2,] 0.1 0.1 0.1 0.1 0.1 [3,] a 0.1 0.1 0.1 0.1 [4,] 0.1 b 0.1 0.1 0.1 [5,] 0.1 b d 0.1 0.1 Later on, user code will set values, e.g., a- rnorm(1) b- 17 d- 4 Now, push those into BM, convert whole thing to numeric newBM- apply(BM, c(1,2), as.numeric) and use newBM for some big calculation. Then re-set new values for a, b, d, do the same over again. I've been trying lots of variations on parse, substitute, and eval. The most interesting function I learned about this morning was delayedAssign. If I had only to work with one scalar, it does what I want delayedAssign(a, whatA) whatA- 91 a [1] 91 I can't see how to make that work in the matrix context, though. Got ideas? I don't think delayedAssign is what you want: it creates promises, and promises can only be evaluated once. You want language entries in your matrix, and you want to use eval() to evaluate them. (Or character entries, and use Henrik's parseAndEval.) Duncan Murdoch __ 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.
Re: [R] replacing characters in matrix. substitute, delayedAssign, huh?
Henrik's proposal works well, so far. Thanks very much. I could not have figured that out (without much more suffering). Here's the working example in case future googlers find their way to this thread. ## Paul Johnson paulj...@ku.edu ## 2012-01-30 ## Special thanks to r-help email list contributors, ## especially Henrik Bengtsson BM - matrix(0.1, 5, 5) BM[2,1] - a BM[3,2] - b BM parseAndEval - function(x, ...) eval(parse(text=x)) a - 0.5 b - 0.4 realBM - apply(BM, MARGIN=c(1,2), FUN=parseAndEval) BM[4,5] - rnorm(1, m=7, sd=1) BM realBM - apply(BM, MARGIN=c(1,2), FUN=parseAndEval) realBM ## Now, what about gui interaction with that table? ## The best nice looking options are not practical at the moment. ## Try this instead data.entry(BM) ## That will work on all platforms, so far as I know, without ## any special effort from us. Run that, make some changes, then ## make sure you insert new R variables to match in your environment. ## Suppose you inserted the letter z in there somewhere ## set z out here z - rpois(1, lambda=10) realBM - apply(BM, MARGIN=c(1,2), FUN=parseAndEval) -- Paul E. Johnson Professor, Political Science 1541 Lilac Lane, Room 504 University of Kansas __ 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.
[R] Replacing characters
Hello guys! May be I am lazy but I need to replace a character like \ or ' or to escape them in a character vector to write a SQL statement. How can I do that? Caveman [[alternative HTML version deleted]] __ 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.
Re: [R] Replacing characters
On Aug 10, 2010, at 2:02 AM, Orvalho Augusto wrote: Hello guys! May be I am lazy but I need to replace a character like \ or ' or to escape them in a character vector to write a SQL statement. ?sub ?regex How can I do that? Caveman -- David Winsemius, MD West Hartford, CT __ 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.
Re: [R] Replacing characters
Try this: library(RMySQL) #conn - dbConnect(...) mysqlEscapeStrings(conn, tes't) On Tue, Aug 10, 2010 at 3:02 AM, Orvalho Augusto orvaq...@gmail.com wrote: Hello guys! May be I am lazy but I need to replace a character like \ or ' or to escape them in a character vector to write a SQL statement. How can I do that? Caveman [[alternative HTML version deleted]] __ 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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O [[alternative HTML version deleted]] __ 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.
[R] replacing characters in formulae / models
Dear all, How can I replace text in objects that are of class formula? y=a * x + b class(y)=formula grep(x,y) y[1] Suppose I would like to replace the x by w in the formula object y. How can this be done? Somehow, the methods that can be used in character objects do not work 1:1 in formula objects... Many thanks and best wishes Christoph -- Dr. rer.nat. Christoph Scherber University of Goettingen DNPW, Agroecology Waldweg 26 D-37073 Goettingen Germany phone +49 (0)551 39 8807 fax +49 (0)551 39 8806 Homepage http://www.gwdg.de/~cscherb1 __ 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.
Re: [R] replacing characters in formulae / models
On Thu, 6 Nov 2008, Christoph Scherber wrote: Dear all, How can I replace text in objects that are of class formula? y=a * x + b class(y)=formula grep(x,y) y[1] What exactly are you trying to accomplish?? And why did you assign 'formula' as the class of a character string? 'y' is not a valid formula object: lm(y) Error in terms.formula(formula, data = data) : argument is not a valid model = Perhaps, you need to review ?formula and 11 Statistical models in R from Introduction to R. Oh, yes. There is the matter of reviewing the _posting guide_ before posting, too. HTH, Chuck Suppose I would like to replace the x by w in the formula object y. How can this be done? Somehow, the methods that can be used in character objects do not work 1:1 in formula objects... Many thanks and best wishes Christoph -- Dr. rer.nat. Christoph Scherber University of Goettingen DNPW, Agroecology Waldweg 26 D-37073 Goettingen Germany phone +49 (0)551 39 8807 fax +49 (0)551 39 8806 Homepage http://www.gwdg.de/~cscherb1 __ 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. Charles C. Berry(858) 534-2098 Dept of Family/Preventive Medicine E mailto:[EMAIL PROTECTED] UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ 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.
Re: [R] replacing characters in formulae / models
Dear all, Dear Keith, Well, of course in fact the problem is more complicated than that. The example was just for illustration. I have several statistical models for which I want to retrieve predictions using delta.method (from library(alr3)). Now for this I need a character string such as e.g. delta.method(model, a + exp(c * x)) # model could for example be an exponential nls fit where the x shall be replaced by a numeric value at which the predictions plus s.e. shall be returned: delta.method(model, a + exp(c * 10)) ## Because there are many models for which this shall be done, I would like to have a function that does something like the following: extract.estimates=function(model.list,xval) for (i in 1:length(model.list)){ mod=model.list[i] - extract the model formula using formula(mod) - every time the variable x is present, it shall be replaced by xval - create a pasted character string pastestring to be used by delta.method result=list(delta.method(mod,pastestring)) return(result) } I hope this has helped illustrating my point. All the best Christoph Keith Jewell schrieb: Hi, Firstly, I'd recommend using '-' for assignment, rather than '='; it saves confusion Secondly, I don't think you want 'a*x+b' as a formula, I think you want an expression. Thirdly, your 'y' has only one term, a 9 character constant = a * x + b Consider instead, y - expression(a*x+b) a - 2 b - 3 x - 1:10 y eval(y) Now, how to replace 'x' by 'w'? I'm not an expert, but this is the kind of thing I need to do, so I'd welcome criticism of my approach. I would view the expression as a list: as.list(y) as.list(y[[1]]) So y is an expression containing a sub-expression; that is y is '(a*x) + b' You want to access the sub-expression 'a*x' y[[1]][[2]] as.list(y[[1]][[2]]) Now you want to replace the third item in that sub-expression with the name (not the character) w y[[1]][[2]][[3]] - as.name(w) w - 11:20 y eval(y) hth Keith J P.S. Perhaps you really do want a formula; y ~ a*x+b ?? In that case I'd still probably manipulate it as a list. --- Christoph Scherber [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Dear all, How can I replace text in objects that are of class formula? y=a * x + b class(y)=formula grep(x,y) y[1] Suppose I would like to replace the x by w in the formula object y. How can this be done? Somehow, the methods that can be used in character objects do not work 1:1 in formula objects... Many thanks and best wishes Christoph -- Dr. rer.nat. Christoph Scherber University of Goettingen DNPW, Agroecology Waldweg 26 D-37073 Goettingen Germany phone +49 (0)551 39 8807 fax +49 (0)551 39 8806 Homepage http://www.gwdg.de/~cscherb1 __ 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. . -- Dr. rer.nat. Christoph Scherber University of Goettingen DNPW, Agroecology Waldweg 26 D-37073 Goettingen Germany phone +49 (0)551 39 8807 fax +49 (0)551 39 8806 Homepage http://www.gwdg.de/~cscherb1 __ 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.