emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [Org-Babel] and R... non-numeric cells
@ 2010-08-12 15:06 Sébastien Vauban
  2010-08-12 15:20 ` Erik Iverson
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Sébastien Vauban @ 2010-08-12 15:06 UTC (permalink / raw)
  To: emacs-orgmode-mXXj517/zsQ

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(@-I..@-II);%.2f::@5$3=vsum(@-I..@-II);%.2f::@5$4=vsum(@-I..@-II);%.2f::@5$5=vsum(@-I..@-II);%.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.
Emacs-orgmode-mXXj517/zsQ@public.gmane.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

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

* Re: [Org-Babel] and R... non-numeric cells
  2010-08-12 15:06 [Org-Babel] and R... non-numeric cells Sébastien Vauban
@ 2010-08-12 15:20 ` Erik Iverson
  2010-08-12 15:30 ` Neil Hepburn
  2010-08-12 20:27 ` Dan Davison
  2 siblings, 0 replies; 6+ messages in thread
From: Erik Iverson @ 2010-08-12 15:20 UTC (permalink / raw)
  To: Sébastien Vauban; +Cc: emacs-orgmode

Hello,

I'm *guessing* that this is more likely an issue of R than of org-mode.
Have you tried tangling the code and simply running the scripts through R?

Essentially, the as.matrix function call is returning a character 
matrix, which could mean your object 'alldata' has some factors instead 
of numeric columns.

R 2.7.1 is, by R standards, ancient, so it would not surprise me a bit
if the differences were because of that old version.

Unless you can show that this is somehow org-mode related, I'd construct
a reproducible example and ask R-help.  In any case,  reproducible
example will help in solving the problem.

Good luck!
--Erik

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(@-I..@-II);%.2f::@5$3=vsum(@-I..@-II);%.2f::@5$4=vsum(@-I..@-II);%.2f::@5$5=vsum(@-I..@-II);%.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
> 

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

* Re: [Org-Babel] and R... non-numeric cells
  2010-08-12 15:06 [Org-Babel] and R... non-numeric cells Sébastien Vauban
  2010-08-12 15:20 ` Erik Iverson
@ 2010-08-12 15:30 ` Neil Hepburn
  2010-08-12 15:57   ` Erik Iverson
  2010-08-12 20:27 ` Dan Davison
  2 siblings, 1 reply; 6+ messages in thread
From: Neil Hepburn @ 2010-08-12 15:30 UTC (permalink / raw)
  To: Sébastien Vauban; +Cc: emacs-orgmode

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(@-I..@-II);%.2f::@5$3=vsum(@-I..@-II);%.2f::@5$4=vsum(@-I..@-II);%.2f::@5$5=vsum(@-I..@-II);%.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.
> Emacs-orgmode@gnu.org
> 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 nhepburn@ualberta.ca

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

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

* Re: [Org-Babel] and R... non-numeric cells
  2010-08-12 15:30 ` Neil Hepburn
@ 2010-08-12 15:57   ` Erik Iverson
  0 siblings, 0 replies; 6+ messages in thread
From: Erik Iverson @ 2010-08-12 15:57 UTC (permalink / raw)
  To: Neil Hepburn; +Cc: Sébastien Vauban, emacs-orgmode

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(@-I..@-II);%.2f::@5$3=vsum(@-I..@-II);%.2f::@5$4=vsum(@-I..@-II);%.2f::@5$5=vsum(@-I..@-II);%.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.
>> Emacs-orgmode@gnu.org
>> 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 nhepburn@ualberta.ca
> 
> 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.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

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

* Re: [Org-Babel] and R... non-numeric cells
  2010-08-12 15:06 [Org-Babel] and R... non-numeric cells Sébastien Vauban
  2010-08-12 15:20 ` Erik Iverson
  2010-08-12 15:30 ` Neil Hepburn
@ 2010-08-12 20:27 ` Dan Davison
  2010-08-18  7:15   ` Sébastien Vauban
  2 siblings, 1 reply; 6+ messages in thread
From: Dan Davison @ 2010-08-12 20:27 UTC (permalink / raw)
  To: Sébastien Vauban; +Cc: public-emacs-orgmode-mXXj517/zsQ



Sébastien Vauban <wxhgmqzgwmuf-geNee64TY+gS+FvcfC7Uqw@public.gmane.org>
writes:

> 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:
>
> #+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(@-I..@-II);%.2f::@5$3=vsum(@-I..@-II);%.2f::@5$4=vsum(@-I..@-II);%.2f::@5$5=vsum(@-I..@-II);%.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
>
> 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*

Hi Seb,

The problem is that Org has not automatically recognised that you want
the first row to be treated as column names. The reason is that your
table contains multiple horizontal separator lines. So for this table,
you must use ':colnames yes' to tell Org that your first row contains
column names. See sections 14.8.2.12, 14.8.2.13 and 14.8.2.14 in the
manual.

As Erik pointed out, using str is a very good way to investigate your
data structures. Notice below that your column names have become the
first row of the data frame, and as a consequence the columns of the
data frame have been force to character rather than numeric:

--8<---------------cut here---------------start------------->8---
#+srcname: barplot-investment(ptable = investissement-2010-2013)
#+begin_src R :XXX-file 1-01-investissement-2010-2013.png :exports none :session :results output
str(ptable)
#+end_src

#+results: barplot-investment
: 
: 'data.frame':	5 obs. of  5 variables:
:  $ V1: chr  "" "RFO" "RFO réseau structurant" "Équipements" ...
:  $ V2: chr  "\\s{Année 2010}" "2596376.3" "3804467.0" "1000000.0" ...
:  $ V3: chr  "\\s{Année 2011}" "1500000.0" "6534066.0" "150000.0" ...
:  $ V4: chr  "\\s{Année 2012}" "500000.0" "3804467.0" "50000.0" ...
:  $ V5: chr  "\\s{Année 2013}" "500000.0" "0.0" "50000.0" ...
--8<---------------cut here---------------end--------------->8---


However, if we use :colnames yes then we have

--8<---------------cut here---------------start------------->8---
#+srcname: barplot-investment(ptable = investissement-2010-2013)
#+begin_src R :XXX-file 1-01-investissement-2010-2013.png :exports none :session :results output :colnames yes
str(ptable)
#+end_src

#+results: barplot-investment
: 
: 'data.frame':	4 obs. of  5 variables:
:  $ X              : chr  "RFO" "RFO réseau structurant" "Équipements" "Total (HTVA)"
:  $ X.s.Année.2010.: num  2596376 3804467 1000000 7400843
:  $ X.s.Année.2011.: num  1500000 6534066 150000 8184066
:  $ X.s.Année.2012.: num  500000 3804467 50000 4354467
:  $ X.s.Année.2013.: num  500000 0 50000 550000
--8<---------------cut here---------------end--------------->8---


And in this case you probably want ':rownames yes', which will mean you
no longer need the -1 in the indexing expression in your R code.

--8<---------------cut here---------------start------------->8---
#+srcname: barplot-investment(ptable = investissement-2010-2013)
#+begin_src R :XXX-file 1-01-investissement-2010-2013.png :exports none :session :results output :colnames yes :rownames yes
str(ptable)
options(width=100)
head(ptable)
#+end_src

#+results: barplot-investment
#+begin_example

'data.frame':	4 obs. of  4 variables:
 $ X.s.Année.2010.: num  2596376 3804467 1000000 7400843
 $ X.s.Année.2011.: num  1500000 6534066 150000 8184066
 $ X.s.Année.2012.: num  500000 3804467 50000 4354467
 $ X.s.Année.2013.: num  500000 0 50000 550000
                       X.s.Année.2010. X.s.Année.2011. X.s.Année.2012. X.s.Année.2013.
RFO                            2596376         1500000          500000          500000
RFO réseau structurant         3804467         6534066         3804467               0
Équipements                    1000000          150000           50000           50000
Total (HTVA)                   7400843         8184066         4354467          550000
#+end_example
--8<---------------cut here---------------end--------------->8---


Dan

> As 1M is numeric, the non-numeric operand must be
> =as.matrix(ptable[2:4, -1])=... Verification:
>
>> 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" 
>
> 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:
>
> #+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
>
> and I even just noticed that, instead of complexifying the expression, I can
> simplify it, in my case, as my =ptable= is numeric already:
>
>> 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
>
> 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

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

* Re: [Org-Babel] and R... non-numeric cells
  2010-08-12 20:27 ` Dan Davison
@ 2010-08-18  7:15   ` Sébastien Vauban
  0 siblings, 0 replies; 6+ messages in thread
From: Sébastien Vauban @ 2010-08-18  7:15 UTC (permalink / raw)
  To: emacs-orgmode-mXXj517/zsQ

Hi Dan,

Dan Davison wrote:
> Sébastien Vauban <wxhgmqzgwmuf-geNee64TY+gS+FvcfC7Uqw-XMD5yJDbdMSQIYZ4X/+iSw@public.gmane.orgrg>
> writes:
>>
>> #+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(@-I..@-II);%.2f::@5$3=vsum(@-I..@-II);%.2f::@5$4=vsum(@-I..@-II);%.2f::@5$5=vsum(@-I..@-II);%.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
>>
>> 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*

Thanks a lot for your detailed explanation. It helped a lot.

Best regards,
  Seb

-- 
Sébastien Vauban


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

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

end of thread, other threads:[~2010-08-18  7:15 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-08-12 15:06 [Org-Babel] and R... non-numeric cells Sébastien Vauban
2010-08-12 15:20 ` Erik Iverson
2010-08-12 15:30 ` Neil Hepburn
2010-08-12 15:57   ` Erik Iverson
2010-08-12 20:27 ` Dan Davison
2010-08-18  7:15   ` Sébastien Vauban

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