[R] Data and Variables from Tables

Shawn Way SWay at meco.com
Tue Mar 21 23:18:38 CET 2017


That worked perfectly!

This makes using a large number of values for programming and their documentation significantly easier.

Thank you

Shawn Way, PE

-----Original Message-----
From: Enrico Schumann [mailto:es at enricoschumann.net] 
Sent: Tuesday, March 21, 2017 4:40 PM
To: Shawn Way <SWay at meco.com>
Cc: r-help at r-project.org
Subject: Re: [R] Data and Variables from Tables

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



More information about the R-help mailing list