Digits problem when i have integer and decimal numbers in one column dataframe

Hi everyone,

I would like to fix my non decimal values as integers but i don't achieve it.
I try this way : round(x, digits=0) but still appears like a decimal number due to my last value (a decimal one).


dataframe_test <- data.frame(
      Information = c( "Number 1", # integer
                       "Number 2", # integer
                       "Number 3", # integer
                       "Number 4 "), # decimal
      Total = c(round(11, digits=0),
                round(21, digits=0),
                round(13, digits=0),
                round(11/21, digits=2)
      )
    )

Information Total
1 Number 1 11.00
2 Number 2 21.00
3 Number 3 13.00
4 Number 4 0.52

But still a wrong display, 11 appears 11.00.

I have the same issue if, for example, one element of the column is a character, then all the elements (numbers) become characters/factors...

Is there a trick other than convert numbers in strings character (i use a shiny app, and extract these dataframes in .xls files, so i would like to have numbers when it's.. numbers).
Thanks

If I understand correctly, you want such a data.frame column which has multiple type of elements? Like, one may integer, one float, one character...something like that?

If that is the case, it is not possible. All elements of a column must be of same type. If you pass elements of different types, R will do the coercion itself, i.e. it will convert all elements to one particular type (in this order logical -> integer -> double -> character).

Hope this helps.

Hi,
Thanks for quick answer.

Yes, i would like to have as result :

Information Total
Number1 11
Number2 21
Number3 13
Number4 0.52

11 not 11.00, 21 not 21.00, 13 not 13.00, 0.52 is ok.

So apparently, it's not possible.
Maybe the matrix way instead of dataframe can change anything?

matrix requires not only all items in a column to be of same type, but all items in the matrix to be of the same type.

The thing to remember when working with R is that calculation and presentation are two different activities, and its best not to blurr them. Process your data with as much precision as makes sense to do, when you have gathered results and want to publish them, then you pick up the presentational packages that will render nice printed tables for you, and on those control how the outputs print. Since printing nice formatted numbers to the screen is its own practice.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.