emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] import R data frame into org-mode table


From: Thomas S. Dye
Subject: Re: [O] import R data frame into org-mode table
Date: Wed, 17 Jul 2013 12:48:00 -1000

Rob Stewart <address@hidden> writes:

> I have an R script that generates a data frame, that I export to a CSV
> file. The data looks something like this:
>
>     "","Variant","Xaxis","N","mean","sd","se"
>     "1","line1",10,5,111.11,9.33,3.11
>     "1","line1",20,5,112.11,9.13,3.14
>     "1","line1",30,5,113.11,9.43,3.10
>     "1","line2",10,5,101.11,8.33,2.11
>     "1","line2",20,5,100.11,8.13,2.12
>     "1","line2",30,5,108.11,8.03,2.10
>
> I have an ongoing org-mode document in which I'd like to report this
> data. I'd like to add a table that looks something like this:
>
>     | Variant | X Axis | Y Axis | N | Mean | Standard Deviation |
> Standard Error |
>     
> |----------------------------------------------------------------------------|
>
> The data will change frequently over the coming months. I'd like
> org-mode to auto-magically read a CSV file, to construct my org-table
> on-the-fly. I'd hoping to avoid copying and pasting results each time.
>
> Is there some org-mode or emacs+ESS magic I can use to populate my
> empty table, with source data from my CSV file?
>
> --
> Rob

Hi Rob,

Andreas' post answers your specific question.

In your situation, I typically don't run through a csv file, but just
write the R script so the data frame is returned to the Org mode buffer
as the result of the source code block.

As for the translation of column heads, see the ascii package for R,
which has a print() routine that recognizes Org mode as a type of
output, and which has facilities for formatting column names.

Here is an example from some work in progress that uses a session:

#+property: exports results
#+property: colnames yes
#+property: session *adzes*

#+name: lda-mid-z
#+header: :results output org
#+begin_src R 
  y <- edxrf[edxrf$lab=='UH-Hilo',]
  fit <- lda(as.factor(site) ~  rb_ppm + sr_ppm + y_ppm + zr_ppm + nb_ppm,
             data=y, na.action="na.omit", CV=TRUE)
  adzes <- y$site=='unknown'
  cap <- "Assignment of unknowns to sources"
  z <- 
ascii(cbind(BPBM=y$bpbm[adzes],round(fit$posterior[adzes,],2)),header=T,include.colnames=T,caption=cap)
  print(z,type="org")
  rm(y,fit,adzes,cap,z)
#+end_src

#+RESULTS: lda-mid-z
#+BEGIN_ORG
#+CAPTION: Assignment of unknowns to sources
| BPBM                      | Haleakala | Kilauea | Mauna Kea | Nu`u | Pololu | 
Waiahole | unknown |
|---------------------------+-----------+---------+-----------+------+--------+----------+---------|
| 1985.134.01 adze          | 0         | 0.1     | 0         | 0    | 0      | 
0.64     | 0.27    |
| 50-HA-1349-G-5 adze       | 0         | 0.85    | 0         | 0    | 0      | 
0        | 0.14    |
| 50-HA-900-P24-1 microadze | 0         | 0       | 1         | 0    | 0      | 
0        | 0       |

...

hth,
Tom


-- 
Thomas S. Dye
http://www.tsdye.com



reply via email to

[Prev in Thread] Current Thread [Next in Thread]