# [R] Data and Variables from Tables

Enrico Schumann es at enricoschumann.net
Tue Mar 21 22:39:38 CET 2017

On Tue, 21 Mar 2017, Shawn Way writes:

> I have an org-mode table with the following structure
> that I am pulling into an R data.frame, using the
> sfsmisc package and using xtable to print in org-mode
>
> | Symbol | Value | Units               |
> |----------+-------+-------------------|
> | A             |         1 | kg/hr                    |
> | \beta    |         2 | \frac{m^3}{hr} |
> | G            |     .25 | in                           |
>
> This all works well and looks great.
>
> What I am trying to do is use this to generate
> variables for programming as well.  For example, when
> processed I would have the following variables:
>
> A <- 1
> beta <- 2
> G <- .25
>
> Has anyone done something like this or can someone
> point me in the right direction to do this?
>
> Shawn Way
>

You may be looking for ?assign.

df <- data.frame(Symbol = c("A", "\\beta",  "G"),
Value  = c(  1,        2, 0.25))

## remove backslashes
df[["Symbol"]] <- gsub("\\", "", df[["Symbol"]], fixed = TRUE)

for (i in seq_len(nrow(df)))
assign(df[i, "Symbol"], df[i, "Value"])

But depending on what you want to do, it may be
cleaner/safer to keep the variables from the table
together in a list.

tbl <- as.list(df[["Value"]])
names(tbl) <- df[["Symbol"]]

## $A ## [1] 1 ## ##$beta
## [1] 2
##
## \$G
## [1] 0.25

--
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net