From mboxrd@z Thu Jan 1 00:00:00 1970 From: Erik Iverson Subject: Re: [Org-Babel] and R... non-numeric cells Date: Thu, 12 Aug 2010 10:57:50 -0500 Message-ID: <4C6419FE.3070708@ccbr.umn.edu> References: <871va3hnvj.fsf@mundaneum.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: quoted-printable Return-path: Received: from [140.186.70.92] (port=34351 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OjaAG-0003XG-CE for emacs-orgmode@gnu.org; Thu, 12 Aug 2010 11:57:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OjaAE-0001ji-GN for emacs-orgmode@gnu.org; Thu, 12 Aug 2010 11:57:56 -0400 Received: from walleye.ccbr.umn.edu ([128.101.116.11]:4907) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OjaAE-0001jY-7B for emacs-orgmode@gnu.org; Thu, 12 Aug 2010 11:57:54 -0400 In-Reply-To: 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: Neil Hepburn Cc: =?windows-1252?Q?S=E9bastien_Vauban?= , emacs-orgmode@gnu.org 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=3Dmytbl :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 ho= rizontal lines in the source table. The two ways that I have dealt with t= he problem are to either not have horizontal lines in the table or use so= me R stuff to clean things up. For example, suppose that I have the follo= wing table >=20 > #+TBLNAME: mytbl > |column1|column2| > |------------|-----------| > | 45 | 34 | > | 77 | 56 | >=20 >=20 > 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 ev= erything as numeric. >=20 > #+BEGIN_SRC R :var tbl=3Dmytbl > names(tbl) <- tbl[1,] #renames the variables from V1, V2 etc to what t= hey 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 v= ariable instead of character. > #+END_SRC >=20 > of course, if you have a column of text in the first column (or any col= umn for that matter) you need to adjust your for-loop accordingly. >=20 > I hope this helps. >=20 > -Neil >=20 > On 2010-08-12, at 9:06 AM, S=E9bastien Vauban wrote: >=20 >> 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=3DlSSSS >> | | \s{Ann=E9e 2010} | \s{Ann=E9e 2011} | \s{An= n=E9e 2012} | \s{Ann=E9e 2013} | >> |------------------------+----------------+----------------+----------= ------+----------------| >> | RFO | 2596376.30 | 1500000.00 | 5000= 00.00 | 500000.00 | >> | RFO r=E9seau structurant | 3804467.00 | 6534066.00 | 380= 4467.00 | 0.00 | >> | =C9quipements | 1000000.00 | 150000.00 | 5= 0000.00 | 50000.00 | >> |------------------------+----------------+----------------+----------= ------+----------------| >> | Total (HTVA) | 7400843.30 | 8184066.00 | 43544= 67.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 :s= ession >> 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=E9e", "Montant HTVA (M=80)") >> mcStackedBarplot(alldata, "Investissements", c(2010:2013), ptable[-nro= w(ptable),1], legend.location=3D"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, O= rg 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 >> =3Das.matrix(ptable[2:4, -1])=3D... Verification: >> >> --8<---------------cut here---------------start------------->8--- >>> ptable >> V1 V2 V3 V= 4 >> 1 \\s{Ann=E9e 2010} \\s{Ann=E9e 2011} \\s{Ann=E9= e 2012} >> 2 RFO 2596376.3 1500000.0 500000= .0 >> 3 RFO r=E9seau structurant 3804467.0 6534066.0 38044= 67.0 >> 4 =C9quipements 1000000.0 150000.0 500= 00.0 >> 5 Total (HTVA) 7400843.3 8184066.0 4354467= .0 >> V5 >> 1 \\s{Ann=E9e 2013} >> 2 500000.0 >> 3 0.0 >> 4 50000.0 >> 5 550000.0 >> >>> as.matrix(ptable[2:4, -1]) >> V2 V3 V4 V5 =20 >> 2 "2596376.3" "1500000.0" "500000.0" "500000.0" >> 3 "3804467.0" "6534066.0" "3804467.0" "0.0" =20 >> 4 "1000000.0" "150000.0" "50000.0" "50000.0"=20 >> --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 =3D investissement-2010-2013) >> #+begin_src R :file 1-01-investissement-sva-2010-2013.png :exports non= e :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, nc= ol=3D4) / 1000000 >> axisLabels <- c("Ann=E9e", "Montant HTVA (M=80)") >> mcStackedBarplot(alldata, "Investissements", c(2010:2013), ptable[2:4,= 1], legend.location=3D"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 =3Dptable=3D 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 treatme= nt of >> string and numerics between our 2 machines. >> >> Any idea? >> >> Best regards, >> Seb >> >> --=20 >> S=E9bastien 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 >> >=20 >=20 >=20 > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > Neil Hepburn, Lecturer in Economics > Department of Social Sciences, Augustana Faculty > University of Alberta > 4901-46 Avenue > Camrose, Alberta T4V 2R3 >=20 > Phone (780) 679-1588 > email nhepburn@ualberta.ca >=20 > No trees were harmed in creating this message. (However, millions of el= ectrons were terribly disturbed.) >=20 >=20 > _______________________________________________ > 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