Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
601 views
in Technique[技术] by (71.8m points)

r - Rounding the numeric values in a dplyr tbl_df upon printing

I'm trying to keep the true values in a dplyr table (tbl_df) but display rounded versions. I feel like there must be a printing method argument to do this.

Here's an example of what I want:

my_tbl <- iris %>% group_by(Species) %>% summarise_each(funs((sum(.*12345e20))))

Instead of this:

print(my_tbl)
Source: local data frame [3 x 5]

     Species Sepal.Length  Sepal.Width Petal.Length  Petal.Width
      (fctr)        (dbl)        (dbl)        (dbl)        (dbl)
1     setosa 3.089954e+26 2.115933e+26 9.024195e+25 1.518435e+25
2 versicolor 3.663996e+26 1.709783e+26 2.629485e+26 8.184735e+25
3  virginica 4.066443e+26 1.835702e+26 3.426972e+26 1.250549e+26

I want something like

print(my_tbl, signif=3)
Source: local data frame [3 x 5]

     Species Sepal.Length Sepal.Width Petal.Length Petal.Width
      (fctr)        (dbl)       (dbl)        (dbl)       (dbl)
1     setosa     3.09e+26    2.12e+26     9.02e+25    1.52e+25
2 versicolor     3.66e+26    1.71e+26     2.63e+26    8.18e+25
3  virginica     4.07e+26    1.84e+26     3.43e+26    1.25e+26
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use print.data.frame instead of print after adjusting the digits option.

options(digits = 3)
print.data.frame(my_tbl)

    Species Sepal.Length Sepal.Width Petal.Length Petal.Width
     setosa     3.09e+26    2.12e+26     9.02e+25    1.52e+25
 versicolor     3.66e+26    1.71e+26     2.63e+26    8.18e+25
  virginica     4.07e+26    1.84e+26     3.43e+26    1.25e+26

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...