[R] Data and Variables from Tables

Shawn Way SWay at meco.com
Wed Mar 22 14:06:43 CET 2017


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:jdnewmil at dcn.davis.ca.us] 
Sent: Tuesday, March 21, 2017 5:40 PM
To: r-help at r-project.org; Shawn Way <SWay at meco.com>; Enrico Schumann <es at enricoschumann.net>
Cc: r-help at r-project.org
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 <SWay at meco.com> 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: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
>
>______________________________________________
>R-help at r-project.org 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.


More information about the R-help mailing list