Deepthi Theresa
2015-Nov-06 21:26 UTC
[R] Dataframes - Integer and decimal in same column?
Hi all, My question is about R dataframes. I am making html reports using R datframe tables and RMarkdown. I have a dataframe with integer values on it and I had to rbind another dataframe with decimal values with the first dataframe. After the rbind function all values changed to decimal values. Can we keep integer and decimal numbers in the same column? Or at least round some of the decimal rows to zero decimal point. anything works. I just want to show the integer rows as integer values and decimal rows as decimal values. Thanks, Deepthi ---------------------------------------------------------------------------------------------------------------- Deepthi Theresa Thomas Kannanayakal Email: deepthitheresa at gmail.com [[alternative HTML version deleted]]
On 06/11/2015 3:26 PM, Deepthi Theresa wrote:> Hi all, > > My question is about R dataframes. I am making html reports using R > datframe tables and RMarkdown. > > I have a dataframe with integer values on it and I had to rbind another > dataframe with decimal values with the first dataframe. > > After the rbind function all values changed to decimal values. Can we keep > integer and decimal numbers in the same column? Or at least round some of > the decimal rows to zero decimal point. anything works. I just want to > show the integer rows as integer values and decimal rows as decimal > values. >There are two issues here. There is the type of the value, and the format for displaying it. You can't mix integer type data and floating point data in the same column. The integer values will be converted to floating point. R by default displays floating point values with the same number of decimal places throughout the column, so data.frame(x = c(1, 1.1)) will display as x 1 1.0 2 1.1 and I think this is what you want to avoid. There are a few ways to do this, but the easiest is to convert the column to character using whatever format you want, e.g. data.frame(y = c(as.character(1), as.character(1.1))) will display as y 1 1 2 1.1 If the numbers are already in the same column, you could do it as data.frame(z = sprintf("%g", c(1, 1.1))) Duncan Murdoch
It is a very fundamental fact about data frames that they are COLUMNS of like data. If you have specific row-oriented requirements then you will need to build a data frame or matrix of character strings of formatted data. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On November 6, 2015 1:26:21 PM PST, Deepthi Theresa <deepthitheresa at gmail.com> wrote:>Hi all, > >My question is about R dataframes. I am making html reports using R >datframe tables and RMarkdown. > >I have a dataframe with integer values on it and I had to rbind another >dataframe with decimal values with the first dataframe. > >After the rbind function all values changed to decimal values. Can we >keep >integer and decimal numbers in the same column? Or at least round some >of >the decimal rows to zero decimal point. anything works. I just want >to >show the integer rows as integer values and decimal rows as decimal >values. > >Thanks, >Deepthi >---------------------------------------------------------------------------------------------------------------- > >Deepthi Theresa Thomas Kannanayakal >Email: deepthitheresa at gmail.com > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
Deepthi Theresa
2015-Nov-06 23:54 UTC
[R] Dataframes - Integer and decimal in same column?
Hello Duncan, Thank you. I completed the task using the exact way you described here. Changed the data frame in to a character type using apply function and complicated the task. Thanks, Deepthi On Nov 6, 2015 4:44 PM, "Duncan Murdoch" <murdoch.duncan at gmail.com> wrote:> On 06/11/2015 3:26 PM, Deepthi Theresa wrote: > >> Hi all, >> >> My question is about R dataframes. I am making html reports using R >> datframe tables and RMarkdown. >> >> I have a dataframe with integer values on it and I had to rbind another >> dataframe with decimal values with the first dataframe. >> >> After the rbind function all values changed to decimal values. Can we >> keep >> integer and decimal numbers in the same column? Or at least round some of >> the decimal rows to zero decimal point. anything works. I just want to >> show the integer rows as integer values and decimal rows as decimal >> values. >> >> > There are two issues here. > > There is the type of the value, and the format for displaying it. > > You can't mix integer type data and floating point data in the same > column. The integer values will be converted to floating point. > > R by default displays floating point values with the same number of > decimal places throughout the column, so data.frame(x = c(1, 1.1)) will > display as > > x > 1 1.0 > 2 1.1 > > and I think this is what you want to avoid. There are a few ways to do > this, but the easiest is to convert the column to character using whatever > format you want, e.g. > > data.frame(y = c(as.character(1), as.character(1.1))) > > will display as > > y > 1 1 > 2 1.1 > > If the numbers are already in the same column, you could do it as > > data.frame(z = sprintf("%g", c(1, 1.1))) > > Duncan Murdoch >[[alternative HTML version deleted]]