0

I have a nasty problem that bugs me a lot.

I have a list (dataframe) that looks like this:

    a            b           c
1   1.00234    1.05667    1.00198

I want to round the numbers of this dataframe to two decimal number. But the trailing zeros have to be kept like the following:

    a            b           c
1   1.00         1.06       1.00

I tried the round and printf() and so on, it doesn't work, because my data is a list. It can't be coerced. However, I'd like to keep my data structure.

Anyone of you know how to solve this? I appreciate it very much!!!

4
  • You have one data.frame? Or a list of data frames? Commented Mar 27, 2014 at 21:04
  • Only one dataframe. data.frame(c(1.00234, 1.05667,1.00198)) like this one. How can you show it as I pointed out above? Commented Mar 27, 2014 at 21:09
  • So a single-column data frame? Your post displays the data as three columns... Commented Mar 27, 2014 at 21:14
  • sorry, it's three columns as it showed above. But that shouldn't be the problem right? You can transpose it if you want. Commented Mar 27, 2014 at 21:18

3 Answers 3

3

Here you go:

df <- data.frame(a=1.00234, b=1.0567, c=1.00198, d=99999, e=.00001)

format(round(df, 2), nsmall=2)
#      a    b    c        d    e
# 1 1.00 1.06 1.00 99999.00 0.00
Sign up to request clarification or add additional context in comments.

2 Comments

Cool. If any of your columns are non-numeric, you'll need to do something more like this: data.frame(lapply(df, function(X) if(is.numeric(X)) format(round(X,2), nsmall=2) else X))
really great explanation. This is too advanced. I need to look into it. awesome man! appreciate it!
1

You can use sapply to effectively loop the rounding over the columns. I'm having trouble determining exactly what your data looks like. But if it's a data.frame with numerous rows and columns, see this example. You'll need to convert back to class data.frame if that matters, as this method of sapply will coerce the data frame to a matrix.

> dd <- data.frame(a = runif(6), b = runif(6), c = rnorm(6))
> dd
          a         b          c
1 0.3992252 0.9905755 -0.2557345
2 0.5052276 0.7990887 -0.7557547
3 0.3215714 0.1134675 -0.4389722
4 0.1794793 0.5372685  1.1657751
5 0.9543305 0.8908360 -1.5966621
6 0.9525730 0.5991279 -0.4819168

> as.data.frame(sapply(dd, round, 2))
     a    b     c
1 0.40 0.99 -0.26
2 0.51 0.80 -0.76
3 0.32 0.11 -0.44
4 0.18 0.54  1.17
5 0.95 0.89 -1.60
6 0.95 0.60 -0.48

1 Comment

Hey man, thank you very much for your effort. Although your answer doesn't really work on my example. But maybe it has to do with my dataframe. Josh's approach worked perfectly. I don't really know why his worked. But I learned a lot from yours too.
0
#include<iostream>
#include<conio.h>
#include<math.h>

using namespace std;

int main()
{
    float a=1.00634;
    float p=round(a*100); float q=p/100;
    printf("%.2f",q);   
    getch();
    return 0;
    }

1 Comment

appreciate your answer, sorry, maybe my bad. My question is R related. not C++.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.