5

I'd like to be able to plot in an orgbabel chunk these data I have in a org-table:

#+NAME:amount
| date             | amount
|------------------+--------|
| <2022-01-30 Sun> |   55.1 | 
| <2022-01-31 Mon> |   54.7 |
| <2022-02-01 Tue> |   54.5 |
| <2022-02-02 Wed> |   55.1 |
| <2022-02-03 Thu> |   54.2 |
| <2022-02-04 Fri> |   54.1 |
| <2022-02-05 Sat> |   53.9 |
| <2022-02-06 Sun> |   54.1 |

The idea is to use these data in a R block and plot amount (y axis) as a function of date (x axis):

#+BEGIN_SRC R 
#+END_SRC

How to reference the data?

Thanks!

NickD
  • 27,023
  • 3
  • 23
  • 42

1 Answers1

6

You can pass the table name to a variable in the R block header:

#+NAME:amount
| date             | amount
|------------------+--------|
| <2022-01-30 Sun> |   55.1 | 
| <2022-01-31 Mon> |   54.7 |
| <2022-02-01 Tue> |   54.5 |
| <2022-02-02 Wed> |   55.1 |
| <2022-02-03 Thu> |   54.2 |
| <2022-02-04 Fri> |   54.1 |
| <2022-02-05 Sat> |   53.9 |
| <2022-02-06 Sun> |   54.1 |


#+begin_src R :var tab=amount
  tab[, 2] <-tab[, 2] * 100
  tab
#+end_src

#+RESULTS:
| <2022-01-30 Sun> | 5510 |
| <2022-01-31 Mon> | 5470 |
| <2022-02-01 Tue> | 5450 |
| <2022-02-02 Wed> | 5510 |
| <2022-02-03 Thu> | 5420 |
| <2022-02-04 Fri> | 5410 |
| <2022-02-05 Sat> | 5390 |
| <2022-02-06 Sun> | 5410 |

Note that your orgmode time stamps will be imported as character strings. If you want to use them as dates, you'll need to convert them in R, maybe with lubridate or something similar.

Update

Working with dates in R is a big topic, and off-topic here. Here's a pointer for converting org timestamps to Date objects:

#+begin_src R :var tab=amount
  tab[, 1] <- as.Date(tab[, 1], tryFormats = "<%Y-%m-%d")
  plot(tab)
#+end_src
NickD
  • 27,023
  • 3
  • 23
  • 42
Tyler
  • 21,719
  • 1
  • 52
  • 92
  • Thanks! Unfortunately my question included the time issue as well. I had a look at lubridate but can't seem to figure it out. – Emmanuel Goldstein Feb 21 '22 at 17:54
  • 1
    No matter what you do on the Emacs side, you need to process the data in R to get it interpreted properly as dates. There are a lot of ways you could approach this, lubridate is just one option. But I think the details are off-topic for this forum, since they don't have anything to do with Emacs. – Tyler Feb 21 '22 at 18:42
  • Thanks, I just am searching but couldn't find how to transform orgmode timestamps into R readable dates. – Emmanuel Goldstein Feb 21 '22 at 18:56
  • 1
    `as.Date("<2022-01-30 Sun>", tryFormats = "<%Y-%m-%d")` – Tyler Feb 21 '22 at 19:02
  • I'm not sure whether it would ever be significant, but I think org expects `` dates to apply to appointments, scheduled tasks and deadlined tasks. I believe `[yyyy-mm-dd]` (entered using `C-c !`) is the preferred format for "inactive" timestamps. I know nothing about R, so I know nothing about the implications if any this format would have for date conversion in R. – Phil Hudson Feb 24 '22 at 11:37
  • @PhilHudson the `tryFormats` argument can contain a vector of possible matching patterns. – Tyler Feb 24 '22 at 15:25