6

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!

1 Answer 1

7

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
6
  • Thanks! Unfortunately my question included the time issue as well. I had a look at lubridate but can't seem to figure it out. Commented Feb 21, 2022 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
    Commented Feb 21, 2022 at 18:42
  • Thanks, I just am searching but couldn't find how to transform orgmode timestamps into R readable dates. Commented Feb 21, 2022 at 18:56
  • 1
    as.Date("<2022-01-30 Sun>", tryFormats = "<%Y-%m-%d")
    – Tyler
    Commented Feb 21, 2022 at 19:02
  • I'm not sure whether it would ever be significant, but I think org expects <yyyy-mm-dd> 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. Commented Feb 24, 2022 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.