[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [O] NA in R source code block
From: |
Jeremie Juste |
Subject: |
Re: [O] NA in R source code block |
Date: |
Sat, 31 Mar 2018 14:37:40 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux) |
Hello,
I don't have an ideal org-mode solution for this problem. I suggesting
two ways hoping that more improvements will come
> Here is an example. The NA in column a shows up in the results as
> nil. Why does that happen? Is there a way of changing this behaviour?
> I can manually replace NA with something else, but doing that in each
> code block is a pain. Since I wrote my first mail, I have written an
> export filter that filters out each “nil” at the time of export and
> replaces it with a “---“. But that is not perhaps the most efficient
> way of doing it.
>
> Warmly,
>
> Vikas
>
> -----------
>
> #+NAME: test
> #+BEGIN_SRC R :results value :exports results :colnames yes :hline yes
>
>
> data.frame(a=c(1,2,NA),b=c("john","dan","marco"))
>
> #+END_SRC
>
> #+RESULTS: test
> | a | b |
>
> |-----+-------|
> | 1 | john |
> | 2 | dan |
> | nil | marco |
## Some suggestions
### Solution 1
You could use an elisp function to clear the nil. It is not automatic
and you would have to write a formula for every column but it might
still be better changing them manually.
I don't know how to implement it automatically though.
#+BEGIN_SRC_elisp
(defun removenil (x)
(interactive)
(replace-regexp-in-string "nil" "" x))
#+END_SRC
#+NAME: test
#+BEGIN_SRC R :results value :exports results :colnames yes :hline yes :dir
/tmp :session R-test1
data.frame(a=c(1,2,NA),b=c("john","dan","marco"))
#+END_SRC
#+RESULTS: test
| a | b |
|---+-------|
| 1 | john |
| 2 | dan |
| | marco |
#+TBLFM: $1='(removenil $1);
### Solution 2
You could easily replace the NA in R before output. For instance
#+NAME: test2
#+BEGIN_SRC R :results value :exports results :colnames yes :hline yes :dir
/tmp :session R-test1
NA_rep <- function(dt,rep="") {
dt[is.na(dt)] <- rep
return(dt)
}
NA_rep(data.frame(a=c(1,2,NA),b=c("john","dan","marco")))
#+END_SRC
#+RESULTS: test2
| a | b |
|---+-------|
| 1 | john |
| 2 | dan |
| | marco |
Best regards,
Jeremie