From mboxrd@z Thu Jan 1 00:00:00 1970 From: Dan Davison Subject: Re: [Org-Babel] and R... non-numeric cells Date: Thu, 12 Aug 2010 16:27:17 -0400 Message-ID: <874oezr2ze.fsf@stats.ox.ac.uk> References: <871va3hnvj.fsf@mundaneum.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from [140.186.70.92] (port=53104 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OjeN9-00031f-0g for emacs-orgmode@gnu.org; Thu, 12 Aug 2010 16:27:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OjeN7-0007uy-Nk for emacs-orgmode@gnu.org; Thu, 12 Aug 2010 16:27:31 -0400 Received: from plane.gmane.org ([80.91.229.3]:54529) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OjeN7-0007ud-Ag for emacs-orgmode@gnu.org; Thu, 12 Aug 2010 16:27:29 -0400 Received: from public by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1OjeN1-0008CQ-LM for emacs-orgmode@gnu.org; Thu, 12 Aug 2010 22:27:23 +0200 In-Reply-To: <871va3hnvj.fsf@mundaneum.com> (=?utf-8?Q?=22S=C3=A9bastien?= Vauban"'s message of "Thu, 12 Aug 2010 17:06:08 +0200") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: =?utf-8?Q?S=C3=A9bastien?= Vauban Cc: public-emacs-orgmode-mXXj517/zsQ@plane.gmane.org S=C3=A9bastien Vauban 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=3DlSSSS > | | \s{Ann=C3=A9e 2010} | \s{Ann=C3=A9e 2011} | \s= {Ann=C3=A9e 2012} | \s{Ann=C3=A9e 2013} | > |------------------------+----------------+----------------+-------------= ---+----------------| > | RFO | 2596376.30 | 1500000.00 | 500000.= 00 | 500000.00 | > | RFO r=C3=A9seau structurant | 3804467.00 | 6534066.00 | 380= 4467.00 | 0.00 | > | =C3=89quipements | 1000000.00 | 150000.00 | 5= 0000.00 | 50000.00 | > |------------------------+----------------+----------------+-------------= ---+----------------| > | Total (HTVA) | 7400843.30 | 8184066.00 | 4354467.= 00 | 550000.00 | > #+TBLFM: @5$2=3Dvsum(@-I..@-II);%.2f::@5$3=3Dvsum(@-I..@-II);%.2f::@5$4= =3Dvsum(@-I..@-II);%.2f::@5$5=3Dvsum(@-I..@-II);%.2f > > whose graphical representation is: > > #+srcname: barplot-investment(ptable =3D investissement-2010-2013) > #+begin_src R :file 1-01-investissement-2010-2013.png :exports none :sess= ion > source("mcplot.R", local=3DTRUE) > ## select the last row only, exclude first column, scale: unit =3D 1M > alldata <- as.matrix(ptable[2:4, -1]) / 1000000 > axisLabels <- c("Ann=C3=A9e", "Montant HTVA (M=E2=82=AC)") > mcStackedBarplot(alldata, "Investissements", c(2010:2013), ptable[-nrow(p= table),1], legend.location=3D"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 =3D investissement-2010-2013) #+begin_src R :XXX-file 1-01-investissement-2010-2013.png :exports none :se= ssion :results output str(ptable) #+end_src #+results: barplot-investment :=20 : 'data.frame': 5 obs. of 5 variables: : $ V1: chr "" "RFO" "RFO r=C3=A9seau structurant" "=C3=89quipements" ... : $ V2: chr "\\s{Ann=C3=A9e 2010}" "2596376.3" "3804467.0" "1000000.0" ... : $ V3: chr "\\s{Ann=C3=A9e 2011}" "1500000.0" "6534066.0" "150000.0" ... : $ V4: chr "\\s{Ann=C3=A9e 2012}" "500000.0" "3804467.0" "50000.0" ... : $ V5: chr "\\s{Ann=C3=A9e 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 =3D investissement-2010-2013) #+begin_src R :XXX-file 1-01-investissement-2010-2013.png :exports none :se= ssion :results output :colnames yes str(ptable) #+end_src #+results: barplot-investment :=20 : 'data.frame': 4 obs. of 5 variables: : $ X : chr "RFO" "RFO r=C3=A9seau structurant" "=C3=89quipe= ments" "Total (HTVA)" : $ X.s.Ann=C3=A9e.2010.: num 2596376 3804467 1000000 7400843 : $ X.s.Ann=C3=A9e.2011.: num 1500000 6534066 150000 8184066 : $ X.s.Ann=C3=A9e.2012.: num 500000 3804467 50000 4354467 : $ X.s.Ann=C3=A9e.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 =3D investissement-2010-2013) #+begin_src R :XXX-file 1-01-investissement-2010-2013.png :exports none :se= ssion :results output :colnames yes :rownames yes str(ptable) options(width=3D100) head(ptable) #+end_src #+results: barplot-investment #+begin_example 'data.frame': 4 obs. of 4 variables: $ X.s.Ann=C3=A9e.2010.: num 2596376 3804467 1000000 7400843 $ X.s.Ann=C3=A9e.2011.: num 1500000 6534066 150000 8184066 $ X.s.Ann=C3=A9e.2012.: num 500000 3804467 50000 4354467 $ X.s.Ann=C3=A9e.2013.: num 500000 0 50000 550000 X.s.Ann=C3=A9e.2010. X.s.Ann=C3=A9e.2011. X.s.Ann=C3= =A9e.2012. X.s.Ann=C3=A9e.2013. RFO 2596376 1500000 500000 = 500000 RFO r=C3=A9seau structurant 3804467 6534066 3804467= 0 =C3=89quipements 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 > =3Das.matrix(ptable[2:4, -1])=3D... Verification: > >> ptable > V1 V2 V3 V4 > 1 \\s{Ann=C3=A9e 2010} \\s{Ann=C3=A9e 2011} \\s{An= n=C3=A9e 2012} > 2 RFO 2596376.3 1500000.0 500000.0 > 3 RFO r=C3=A9seau structurant 3804467.0 6534066.0 38044= 67.0 > 4 =C3=89quipements 1000000.0 150000.0 500= 00.0 > 5 Total (HTVA) 7400843.3 8184066.0 4354467.0 > V5 > 1 \\s{Ann=C3=A9e 2013} > 2 500000.0 > 3 0.0 > 4 50000.0 > 5 550000.0 > >> as.matrix(ptable[2:4, -1]) > V2 V3 V4 V5=20=20=20=20=20=20=20=20 > 2 "2596376.3" "1500000.0" "500000.0" "500000.0" > 3 "3804467.0" "6534066.0" "3804467.0" "0.0"=20=20=20=20=20 > 4 "1000000.0" "150000.0" "50000.0" "50000.0"=20 > > The numerics are written between double quotes... Why!? > > I had temporarily patched the above problem in my document by updating th= e line > with the assignment: > > #+srcname: barplot-investment-sva(ptable =3D investissement-2010-2013) > #+begin_src R :file 1-01-investissement-sva-2010-2013.png :exports none := session > source("mcplot.R", local=3DTRUE) > ## select the last row only, exclude first column, scale: unit =3D 1M > alldata <- matrix(as.numeric(as.matrix(ptable[2:4, -1])), nrow=3D3, ncol= =3D4) / 1000000 > axisLabels <- c("Ann=C3=A9e", "Montant HTVA (M=E2=82=AC)") > mcStackedBarplot(alldata, "Investissements", c(2010:2013), ptable[2:4,1],= legend.location=3D"topright") > #+end_src > > and I even just noticed that, instead of complexifying the expression, I = can > simplify it, in my case, as my =3Dptable=3D 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