From mboxrd@z Thu Jan 1 00:00:00 1970 From: "Charles C. Berry" Subject: Re: Strings converted to numbers in Org table? Date: Mon, 27 Feb 2017 10:17:03 -0800 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:54950) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ciPrf-0004wC-Jg for emacs-orgmode@gnu.org; Mon, 27 Feb 2017 13:17:40 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ciPra-00066b-MA for emacs-orgmode@gnu.org; Mon, 27 Feb 2017 13:17:39 -0500 Received: from iport-acv3-out.ucsd.edu ([132.239.0.4]:49226) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1ciPra-00066I-5Z for emacs-orgmode@gnu.org; Mon, 27 Feb 2017 13:17:34 -0500 In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Vicente Vera Cc: emacs-orgmode On Mon, 27 Feb 2017, Vicente Vera wrote: > Hello again, I'm sorry for being noisy. > > OK yes, `org-babel-read' is indeed converting "number strings" to > numbers. > [proposed fix deleted] I don't have a feeling as to whether the proposed fix is appropriate. However, there are other ways to solve this issue. See below. >>> >>> - In R every value is a string. "var2" contains no numbers (is a >>> character vector). >>> >>> - Upon conversion to a table Org removes the zero from "var2" last >>> value. >>> >>> ------------------------------ >>> >>> #+BEGIN_SRC R :session *mwe* :results value table :colnames yes >>> tst <- data.frame(var1 = c("a", "b", "c", "d", "e", "f", "g"), >>> var2 = c("150", "210", "140", "150", "192", "497", >>> "3.350"), >>> stringsAsFactors = FALSE) >>> tst >>> #+END_SRC >>> >>> #+RESULTS: >>> | var1 | var2 | >>> |------+------| >>> | a | 150 | >>> | b | 210 | >>> | c | 140 | >>> | d | 150 | >>> | e | 192 | >>> | f | 497 | >>> | g | 3.35 | >>> >>> ------------------------------ >>> >>> Here's the output as seen in R: >>> >>> : > tst >>> : var1 var2 >>> : 1 a 150 >>> : 2 b 210 >>> : 3 c 140 >>> : 4 d 150 >>> : 5 e 192 >>> : 6 f 497 >>> : 7 g 3.350 >>> >>> Details on the data frame: >>> >>> : > str(tst) >>> : 'data.frame': 7 obs. of 2 variables: >>> : $ var1: chr "a" "b" "c" "d" ... >>> : $ var2: chr "150" "210" "140" "150" ... >>> >>> It seems Org knows that the values on column "var2" are numbers and >>> converts the strings to numbers, applying some obscure trimming on the >>> digits. The "3.350" value needs to be left as is. IMHO, it is often best to handle formatting of output in the language of the src block. There are some tools for doing this in R: the `ascii' package is one. `xtable' is another. You can always do something like this: #+BEGIN_SRC R :session *mwe* :results output tst <- data.frame(var1 = c("a", "b", "c", "d", "e", "f", "g"), var2 = c("150", "210", "140", "150", "192", "497", "3.350"), stringsAsFactors = FALSE) cat( capture.output( print(tst, row.names=FALSE)), sep="\n") #+END_SRC Or you can use `ox-ravel'[1] to convert to one of the knitr-like formats and let knitr or rmarkdown handle the output conversion. Best, Chuck [1] https://github.com/chasberry/orgmode-accessories