Data and Variables from Tables

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Data and Variables from Tables

Shawn Way-4
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

   

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Data and Variables from Tables

Enrico Schumann-2
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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Data and Variables from Tables

Shawn Way-4
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:[hidden email]]
Sent: Tuesday, March 21, 2017 4:40 PM
To: Shawn Way <[hidden email]>
Cc: [hidden email]
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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Data and Variables from Tables

Jeff Newmiller
He offered two solutions, and I want to second the vote against the first one.

I often put large numbers of configuration variables in a few CSV files organized by topic area and read them in. The resulting data frames are capable of holding multiple cases if desired and I just specify which case (row) I am interested in using and pass around 1-row data frames to the computation functions.
--
Sent from my phone. Please excuse my brevity.

On March 21, 2017 3:18:38 PM PDT, Shawn Way <[hidden email]> wrote:

>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:[hidden email]]
>Sent: Tuesday, March 21, 2017 4:40 PM
>To: Shawn Way <[hidden email]>
>Cc: [hidden email]
>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
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Data and Variables from Tables

Shawn Way-4
I implemented the second as well.  It was much easier to create a function to automate this as well as assign  the results to a single data.frame

Shawn Way, PE

-----Original Message-----
From: Jeff Newmiller [mailto:[hidden email]]
Sent: Tuesday, March 21, 2017 5:40 PM
To: [hidden email]; Shawn Way <[hidden email]>; Enrico Schumann <[hidden email]>
Cc: [hidden email]
Subject: Re: [R] Data and Variables from Tables

He offered two solutions, and I want to second the vote against the first one.

I often put large numbers of configuration variables in a few CSV files organized by topic area and read them in. The resulting data frames are capable of holding multiple cases if desired and I just specify which case (row) I am interested in using and pass around 1-row data frames to the computation functions.
--
Sent from my phone. Please excuse my brevity.

On March 21, 2017 3:18:38 PM PDT, Shawn Way <[hidden email]> wrote:

>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:[hidden email]]
>Sent: Tuesday, March 21, 2017 4:40 PM
>To: Shawn Way <[hidden email]>
>Cc: [hidden email]
>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
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>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.
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.