emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Orgmode] [Org-Babel] and R... non-numeric cells


From: Erik Iverson
Subject: Re: [Orgmode] [Org-Babel] and R... non-numeric cells
Date: Thu, 12 Aug 2010 10:57:50 -0500
User-agent: Thunderbird 2.0.0.23 (X11/20090812)

Which version of org-mode are you using? I do not see what you
claim to see.

What do you get when you run the following:

#+TBLNAME: mytbl
|column1|column2|
|------------|-----------|
|  45         |    34      |
|  77         |    56      |

#+BEGIN_SRC R :var tbl=mytbl :results output
str(tbl)
#+END_SRC


I get the following, which looks right to me.

#+results:
: 'data.frame': 2 obs. of  2 variables:
:  $ column1: int  45 77
:  $ column2: int  34 56


Neil Hepburn wrote:
I have had a similar problem and I traced it back to the presence of horizontal 
lines in the source table. The two ways that I have dealt with the problem are 
to either not have horizontal lines in the table or use some R stuff to clean 
things up. For example, suppose that I have the following table

#+TBLNAME: mytbl
|column1|column2|
|------------|-----------|
|  45         |    34      |
|  77         |    56      |


when I send that to R, it will treat everything as character due to the 
horizontal line.  The following R snippet will clean it up and recast 
everything as numeric.

#+BEGIN_SRC R :var tbl=mytbl
names(tbl) <- tbl[1,]  #renames the variables from V1, V2 etc to what they 
should be
tbl <- tb[-1,] #gets rid of the first row that had the errant variable names
for (i in 1:ncol(tbl)){
tbl[,i] <- as.numeric(tbl[,i])
} # the for-loop goes through each column and recasts it as a numeric variable 
instead of character.
#+END_SRC

of course, if you have a column of text in the first column (or any column for 
that matter) you need to adjust your for-loop accordingly.

I hope this helps.

-Neil

On 2010-08-12, at 9:06 AM, Sébastien Vauban wrote:

Hello,

For a report I'm writing, I've been helped by a colleague of mine (let's call
him Albert) for the R graphics generation.

Here's an extract of my doc:

--8<---------------cut here---------------start------------->8---
#+TBLNAME: investissement-2010-2013
#+ATTR_LaTeX: align=lSSSS
|                        | \s{Année 2010} | \s{Année 2011} | \s{Année 2012} | 
\s{Année 2013} |
|------------------------+----------------+----------------+----------------+----------------|
| RFO                    |     2596376.30 |     1500000.00 |      500000.00 |   
   500000.00 |
| RFO réseau structurant |     3804467.00 |     6534066.00 |     3804467.00 |   
        0.00 |
| Équipements            |     1000000.00 |      150000.00 |       50000.00 |   
    50000.00 |
|------------------------+----------------+----------------+----------------+----------------|
| Total (HTVA)           |     7400843.30 |     8184066.00 |     4354467.00 |   
   550000.00 |
#+TBLFM: 
@5$2=vsum(@address@hidden);%.2f::@5$3=vsum(@address@hidden);%.2f::@5$4=vsum(@address@hidden);%.2f::@5$5=vsum(@address@hidden);%.2f

whose graphical representation is:

#+srcname: barplot-investment(ptable = investissement-2010-2013)
#+begin_src R :file 1-01-investissement-2010-2013.png :exports none :session
source("mcplot.R", local=TRUE)
## select the last row only, exclude first column, scale: unit = 1M
alldata <- as.matrix(ptable[2:4, -1]) / 1000000
axisLabels <- c("Année", "Montant HTVA (M€)")
mcStackedBarplot(alldata, "Investissements", c(2010:2013), ptable[-nrow(ptable),1], 
legend.location="topright")
#+end_src
--8<---------------cut here---------------end--------------->8---

That works perfectly for him (on Ubuntu 9.04, R 2.7.1, Emacs 22.2.1, Org 6.35)

Not for me... on Ubuntu 10.04, R 2.10.1, Emacs 23.1.1, Org 7.01, ESS 5.10: I
get the message

   *Error in as.matrix(ptable[2:4, -1])/1e+06 :
    non-numeric argument to binary operator*

As 1M is numeric, the non-numeric operand must be
=as.matrix(ptable[2:4, -1])=... Verification:

--8<---------------cut here---------------start------------->8---
ptable
                     V1              V2              V3              V4
1                        \\s{Année 2010} \\s{Année 2011} \\s{Année 2012}
2                    RFO       2596376.3       1500000.0        500000.0
3 RFO réseau structurant       3804467.0       6534066.0       3804467.0
4            Équipements       1000000.0        150000.0         50000.0
5           Total (HTVA)       7400843.3       8184066.0       4354467.0
              V5
1 \\s{Année 2013}
2        500000.0
3             0.0
4         50000.0
5        550000.0

as.matrix(ptable[2:4, -1])
V2 V3 V4 V5 2 "2596376.3" "1500000.0" "500000.0" "500000.0" 3 "3804467.0" "6534066.0" "3804467.0" "0.0" 4 "1000000.0" "150000.0" "50000.0" "50000.0" --8<---------------cut here---------------end--------------->8---

The numerics are written between double quotes... Why!?

I had temporarily patched the above problem in my document by updating the line
with the assignment:

--8<---------------cut here---------------start------------->8---
#+srcname: barplot-investment-sva(ptable = investissement-2010-2013)
#+begin_src R :file 1-01-investissement-sva-2010-2013.png :exports none :session
source("mcplot.R", local=TRUE)
## select the last row only, exclude first column, scale: unit = 1M
alldata <- matrix(as.numeric(as.matrix(ptable[2:4, -1])), nrow=3, ncol=4) / 
1000000
axisLabels <- c("Année", "Montant HTVA (M€)")
mcStackedBarplot(alldata, "Investissements", c(2010:2013), ptable[2:4,1], 
legend.location="topright")
#+end_src
--8<---------------cut here---------------end--------------->8---

and I even just noticed that, instead of complexifying the expression, I can
simplify it, in my case, as my =ptable= is numeric already:

--8<---------------cut here---------------start------------->8---
ptable[2:4, -1]
        V2        V3        V4       V5
2 2596376.3 1500000.0  500000.0 500000.0
3 3804467.0 6534066.0 3804467.0      0.0
4 1000000.0  150000.0   50000.0  50000.0
--8<---------------cut here---------------end--------------->8---

But I still don't understand what is reponsible of a different treatment of
string and numerics between our 2 machines.

Any idea?

Best regards,
 Seb

--
Sébastien Vauban


_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
address@hidden
http://lists.gnu.org/mailman/listinfo/emacs-orgmode




=============================================
Neil Hepburn, Lecturer in Economics
Department of Social Sciences, Augustana Faculty
University of Alberta
4901-46 Avenue
Camrose, Alberta  T4V 2R3

Phone (780) 679-1588
email address@hidden

No trees were harmed in creating this message. (However, millions of electrons 
were terribly disturbed.)


_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
address@hidden
http://lists.gnu.org/mailman/listinfo/emacs-orgmode



reply via email to

[Prev in Thread] Current Thread [Next in Thread]