[R] replacing characters in matrix. substitute, delayedAssign, huh?

Paul Johnson pauljohn32 at gmail.com
Mon Jan 30 22:03:37 CET 2012


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 <pauljohn at 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



More information about the R-help mailing list