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.