Espejito
Espejito

Reputation: 480

R: How to round values of a data frame

I have a simple data frame with values that I want to round to one decimal. I tried this code with little success.

df %>% 
  mutate_if(is.numeric, round, digits = 1)

That code sure gives me rounded values in the console and in a plot if used within ggplot, but is there a way I could easily get rounded values into the column "mean" of the following data frame?

structure(list(question = c("Q1", "Q10", "Q11", "Q12", "Q2", 
"Q3", "Q4", "Q5", "Q6", "Q7", "Q8", "Q9"), n = c(204L, 204L, 
204L, 204L, 204L, 204L, 204L, 204L, 204L, 204L, 204L, 204L), 
    mean = c(5.22549019607843, 4.87684729064039, 4.95098039215686, 
    4.39705882352941, 5.47058823529412, 5.51470588235294, 4.50490196078431, 
    4.92647058823529, 4.40686274509804, 5.56862745098039, 5.56372549019608, 
    5.23529411764706), sd = c(1.1524816893289, 1.21443704569259, 
    1.31214449357814, 1.5422430010719, 1.12039650223724, 1.15104553532809, 
    1.37714471881058, 1.34621721218454, 1.30030385262334, 0.871099231072865, 
    0.830963499839951, 1.36945187401243)), row.names = c(NA, 
12L), class = c("tbl_df", "tbl", "data.frame"))

Upvotes: 1

Views: 3533

Answers (3)

sashahafner
sashahafner

Reputation: 454

The rounddf() function available in the jumbled repo (https://github.com/sashahafner/jumbled) does this, perhaps more simply than other options. It can use round() or signif() (or other functions) and you can specify a different number of digits for each column See below for three examples with your data.

> dfr <- rounddf(df, 1)
> dfr
# A tibble: 12 × 4
   question     n  mean    sd
   <chr>    <int> <dbl> <dbl>
 1 Q1         204   5.2   1.2
 2 Q10        204   4.9   1.2
 3 Q11        204   5     1.3
 4 Q12        204   4.4   1.5
 5 Q2         204   5.5   1.1
 6 Q3         204   5.5   1.2
 7 Q4         204   4.5   1.4
 8 Q5         204   4.9   1.3
 9 Q6         204   4.4   1.3
10 Q7         204   5.6   0.9
11 Q8         204   5.6   0.8
12 Q9         204   5.2   1.4

> dfr <- rounddf(df, digits = 3, func = signif)
> dfr
# A tibble: 12 × 4
   question     n  mean    sd
   <chr>    <int> <dbl> <dbl>
 1 Q1         204  5.23 1.15
 2 Q10        204  4.88 1.21
 3 Q11        204  4.95 1.31
 4 Q12        204  4.4  1.54
 5 Q2         204  5.47 1.12
 6 Q3         204  5.51 1.15
 7 Q4         204  4.5  1.38
 8 Q5         204  4.93 1.35
 9 Q6         204  4.41 1.3
10 Q7         204  5.57 0.871
11 Q8         204  5.56 0.831
12 Q9         204  5.24 1.37

> dfr <- rounddf(df, digits = c(0, 0, 1, 2), func = round)
> dfr
# A tibble: 12 × 4
   question     n  mean    sd
   <chr>    <int> <dbl> <dbl>
 1 Q1         204   5.2  1.15
 2 Q10        204   4.9  1.21
 3 Q11        204   5    1.31
 4 Q12        204   4.4  1.54
 5 Q2         204   5.5  1.12
 6 Q3         204   5.5  1.15
 7 Q4         204   4.5  1.38
 8 Q5         204   4.9  1.35
 9 Q6         204   4.4  1.3
10 Q7         204   5.6  0.87
11 Q8         204   5.6  0.83
12 Q9         204   5.2  1.37

Upvotes: 0

TarJae
TarJae

Reputation: 78917

We could use across from dplyr package. And note mutate_if is deprecated https://www.rdocumentation.org/packages/tidytable/versions/0.5.8/topics/mutate_if.

df %>% 
    dplyr::mutate(across(where(is.numeric), round, 1))

Output:

# A tibble: 12 x 4
   question     n  mean    sd
 * <chr>    <dbl> <dbl> <dbl>
 1 Q1         204   5.2   1.2
 2 Q10        204   4.9   1.2
 3 Q11        204   5     1.3
 4 Q12        204   4.4   1.5
 5 Q2         204   5.5   1.1
 6 Q3         204   5.5   1.2
 7 Q4         204   4.5   1.4
 8 Q5         204   4.9   1.3
 9 Q6         204   4.4   1.3
10 Q7         204   5.6   0.9
11 Q8         204   5.6   0.8
12 Q9         204   5.2   1.4

Upvotes: 3

Tsch&#246;si
Tsch&#246;si

Reputation: 579

You need to write to the mean column of your df.

df$mean <- round(df$mean, digits = 1)

Upvotes: 0

Related Questions