Paul Johnson
2012-Jan-30 18:26 UTC
[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" > BMvar1 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
Richard M. Heiberger
2012-Jan-30 18:31 UTC
[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 <pauljohn32@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Gabor Grothendieck
2012-Jan-30 18:40 UTC
[R] replacing characters in matrix. substitute, delayedAssign, huh?
On Mon, Jan 30, 2012 at 1:26 PM, Paul Johnson <pauljohn32 at 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
Henrik Bengtsson
2012-Jan-30 18:45 UTC
[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 <pauljohn32 at 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 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.
Duncan Murdoch
2012-Jan-30 18:58 UTC
[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