emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* NA in R source code block
@ 2018-03-29 11:35 Vikas Rawal
  2018-03-31  6:47 ` Jeremie Juste
  0 siblings, 1 reply; 5+ messages in thread
From: Vikas Rawal @ 2018-03-29 11:35 UTC (permalink / raw)
  To: org-mode mailing list

NAs in a data frame that is created by an R source code block show up in the results as nil. Can I change it to something else (say, “—“)?

Vikas

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: NA in R source code block
  2018-03-29 11:35 NA in R source code block Vikas Rawal
@ 2018-03-31  6:47 ` Jeremie Juste
  2018-03-31  7:57   ` Vikas Rawal
  0 siblings, 1 reply; 5+ messages in thread
From: Jeremie Juste @ 2018-03-31  6:47 UTC (permalink / raw)
  To: Vikas Rawal; +Cc: org-mode mailing list

Vikas Rawal <vikaslists@agrarianresearch.org> writes:
Hello,

> NAs in a data frame that is created by an R source code block show up in the results as nil. Can I change it to something else (say, “—“)?

Could you elaborate on your issue? Can you give a minimal example of
your block? It is very difficult to help with the information you gave.

Best regards,
Jeremie

PS: feel free to say Hi :-)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: NA in R source code block
  2018-03-31  6:47 ` Jeremie Juste
@ 2018-03-31  7:57   ` Vikas Rawal
  2018-03-31 12:37     ` Jeremie Juste
  0 siblings, 1 reply; 5+ messages in thread
From: Vikas Rawal @ 2018-03-31  7:57 UTC (permalink / raw)
  To: Jeremie Juste, org-mode mailing list


> 
>> NAs in a data frame that is created by an R source code block show up in the results as nil. Can I change it to something else (say, “—“)?
> 
> Could you elaborate on your issue? Can you give a minimal example of
> your block? It is very difficult to help with the information you gave.
> 

Hi and Greetings! And apologies if my previous mail sounded rude. That was surely not my intent.

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 |

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: NA in R source code block
  2018-03-31  7:57   ` Vikas Rawal
@ 2018-03-31 12:37     ` Jeremie Juste
  2018-03-31 12:43       ` Vikas Rawal
  0 siblings, 1 reply; 5+ messages in thread
From: Jeremie Juste @ 2018-03-31 12:37 UTC (permalink / raw)
  To: Vikas Rawal; +Cc: org-mode mailing list


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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: NA in R source code block
  2018-03-31 12:37     ` Jeremie Juste
@ 2018-03-31 12:43       ` Vikas Rawal
  0 siblings, 0 replies; 5+ messages in thread
From: Vikas Rawal @ 2018-03-31 12:43 UTC (permalink / raw)
  To: Jeremie Juste; +Cc: org-mode mailing list

[-- Attachment #1: Type: text/plain, Size: 1301 bytes --]


> 
> ### 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
> 


I created this export filter which uses something similar but automatically replaces nil with --- on all my documents:

(defun org-export-blanks-filter-latex (row backend info)
  "Replace nil in table with --- in LaTeX export."
  (when (org-export-derived-backend-p backend 'latex)
    (replace-regexp-in-string "nil" "---" row)))

(add-to-list 'org-export-filter-table-row-functions
             'org-export-blanks-filter-latex)


> #+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
> 

This gets more painful if I want to use something like “—“ in numeric columns.

The first approach/export filter is less painful than this.

Thanks,

Vikas



[-- Attachment #2: Type: text/html, Size: 14921 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-03-31 12:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-29 11:35 NA in R source code block Vikas Rawal
2018-03-31  6:47 ` Jeremie Juste
2018-03-31  7:57   ` Vikas Rawal
2018-03-31 12:37     ` Jeremie Juste
2018-03-31 12:43       ` Vikas Rawal

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).