emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Dan Davison <davison@stats.ox.ac.uk>
To: Graham Smith <myotisone@gmail.com>
Cc: emacs-orgmode@gnu.org
Subject: Re: [babel] Babel - importing a data file- missing header row
Date: Tue, 08 Dec 2009 12:46:53 -0500	[thread overview]
Message-ID: <871vj573zm.fsf@stats.ox.ac.uk> (raw)
In-Reply-To: <2c75873c0912080841j39a13a55t35c737be3455e211@mail.gmail.com> (Graham Smith's message of "Tue, 8 Dec 2009 16:41:22 +0000")

Graham Smith <myotisone@gmail.com> writes:

> I need a bit of help importing a data file into orgmode/babel.
>
> The import doesn't seem to know about having a header row
>

Hi Graham,

Could you post the first few lines of your data file, or a small example
with the same format, as I don't completely understand the output you
are getting.

But try using the ':colnames t' header argument. For example, if I have
this data file (space-separated, header line has same num entries as
data rows).

-----------------------------
ID wood A.reptans E.amygdaloides   
a wood1 5 10
b wood2 1 0
-----------------------------

Then I get

#+srcname:woodland
#+begin_src R
read.table("/tmp/woodlands.txt",header=TRUE)
#+end_src

#+results: woodland
| "a" | "wood1" | 5 | 10 |
| "b" | "wood2" | 1 |  0 |

And if I use the colnames header argument I get

#+srcname:woodland
#+begin_src R :colnames t
read.table("/tmp/woodlands.txt",header=TRUE)
#+end_src

#+results: woodland
| "ID" | "wood"  | "A.reptans" | "E.amygdaloides" |
|------+---------+-------------+------------------|
| "a"  | "wood1" |           5 |               10 |
| "b"  | "wood2" |           1 |                0 |

Also, I recommend working with the :session header argument. Then after
you evaluate an org-babel block, you can go to the ESS session and
examine the R objects it has created. You'd need to save the data frame
to a variable of course, so something like this

#+srcname:woodland
#+begin_src R :colnames t :session
  woodlands <- read.table("/tmp/woodlands.txt",header=TRUE)
  woodlands
#+end_src

Now if I switch to the *R* buffer, I can see what it has done:


First you'll see some lines like this; these reflect what org-babel did

-----------------------------------------------------------------------------------------------------------
> 
woodlands <- read.table("/tmp/woodlands.txt",header=TRUE)
write.table(.Last.value, file="/tmp/org-babel-R6645iE0", sep="\t", na="nil",row.names=FALSE, col.names=TRUE, quote=FALSE)
'org_babel_R_eoe'

> woodlands <- read.table("/tmp/woodlands.txt",header=TRUE)
> write.table(.Last.value, file="/tmp/org-babel-R6645iE0", sep="\t", na="nil",row.names=FALSE, col.names=TRUE, quote=FALSE)
> 'org_babel_R_eoe'         
[1] "org_babel_R_eoe"
-----------------------------------------------------------------------------------------------------------\
Ignore the org_babel_R_eoe stuff, that's to do with org-babel internals.

Now you can check that your variable looks sensible:

-----------------------------------------------------------------------------------------------------------\
> woodlands
  ID  wood A.reptans E.amygdaloides
1  a wood1         5             10
2  b wood2         1              0
> 
-----------------------------------------------------------------------------------------------------------\

Dan

p.s. Please include the string "[babel]" in the subject line!

> #+srcname:woodland
> #+begin_src R
> read.table("/home/graham/Dropbox/College/BY4001/WoodlandData/BY4001WoodlandDataFinal.txt",header=TRUE)
> #+end_src
>
> #+resname: woodland
> | "V1.1"  | "v" | 0 | 0 | 0 |  0 | 0 |  0 |  0 |  0 |  0 |  0 |  0 |
> 0 |  0 |  0 | 0 |  0 | 0 |  0 |  0 |  0 |  0 | 0 |  0 | 0 | 0 |  0 |
> 0 |  0 | 0 |  0 |  5 |  0 |  0 | 10 |  0 | 0 |  0 | 100 |  0 |  3 | B
>
>
> But strangely if I change the command and run the names(woodland)
> after the import, I get a table of variable names but no data. I'm not
> sure if the data is there but hidden as I can't get any commands to
> work on the woodland object.
>
> * Woodland Data
> #+srcname:woodland
> #+begin_src R
> woodland<-read.table("/home/graham/Dropbox/College/BY4001/WoodlandData/BY4001WoodlandDataFinal.txt",header=TRUE)
> names(woodland)
> #+end_src
>
> #+resname: woodland
> | "ID"           |
> | "wood"         |
> | "Ajug.Rept"    |
> | "Ange.sylv"    |
> | "Brac.sylv"    |
> | "Brom.ramo"    |
> | "Care.pend"    |
> | "Care.remo"    |
> | "Care.sylv"    |
> | "Cirs.palu"    |
> | "Cory.avel"    |
> | "Desc.caes"    |
> | "Dryo.fili"    |
> | "Endy.non"     |
> | "Euph.amyg"    |
>
> So it seems I already need some more help.
>
> Graham
>
>
> _______________________________________________
> Emacs-orgmode mailing list
> Please use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

  reply	other threads:[~2009-12-08 17:47 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-12-08 16:41 Babel - importing a data file- missing header row Graham Smith
2009-12-08 17:46 ` Dan Davison [this message]
2009-12-08 18:08   ` [babel] " Graham Smith

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=871vj573zm.fsf@stats.ox.ac.uk \
    --to=davison@stats.ox.ac.uk \
    --cc=emacs-orgmode@gnu.org \
    --cc=myotisone@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).