r - r - summarise() 中的四舍五入
问题描述
我想在使用group_by()
和summarise()
from package的聚合输出中看到更多数字{dplyr}
。我的代码如下:
library(dplyr)
# download 2 datasets
download.file('https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2FGDP.csv','GDP.csv',mode = 'wb')
GDP<-read.csv('GDP.csv',skip=4,stringsAsFactors = F,na.strings = '')
GDP<-GDP%>%filter(!is.na(X),!is.na(X.1))%>%mutate(X.1=as.numeric(X.1))
download.file('https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2FEDSTATS_Country.csv','EDSTATS.csv',mode = 'wb')
edu<-read.csv('EDSTATS.csv',stringsAsFactors = F)
# join these two datasets
df<-inner_join(GDP,edu,by=c('X'='CountryCode'))%>%arrange(desc(X.1))
# aggregation
df%>%group_by(Income.Group)%>%summarise(avg_GDP=mean(X.1))
我从控制台得到的结果:
# A tibble: 5 x 2
Income.Group avg_GDP
<chr> <dbl>
1 High income: nonOECD 91.9
2 High income: OECD 33.0
3 Low income 134.
4 Lower middle income 108.
5 Upper middle income 92.1
显然,这个数字没有完整显示。那么我怎样才能看到更多的数字avg_GDP
呢?
如果我将结果分配给一个新的数据框并在 RStudio 中查看它,我会看到更多的数字,但仍然只有 5 个数字:
df2<-df%>%group_by(Income.Group)%>%summarise(avg_GDP=mean(X.1))
View(df2)
那么如何在控制台打印和数据框 View() 中看到更多数字呢?
我试过了:
df%>%group_by(Income.Group)%>%summarise(avg_GDP=mean(X.1,digits=10))
它没有用。
我的问题与潜在的重复问题不同的是,我想要可以在 %>% 链中完成这项工作的代码。从他的帖子中,我喜欢以下答案:
# this is my favorite, because it fits well with my original code with %>%.
print.data.frame(my_tbl, digits = 3)
或者
options(digits = 3)
print.data.frame(my_tbl)
从我的帖子来看,我喜欢options(pillar.sigfig = 10)
。
解决方案
对于 tibble 包,您需要修改 option pillar.sigfig
。
pillar.sigfig
:将打印和突出显示的有效位数,默认值:3
library(tibble)
options(pillar.sigfig = 10)
set.seed(1)
tibble(a = rnorm(3), b = rexp(3))
# A tibble: 3 x 2
# a b
# <dbl> <dbl>
#1 -0.6264538107 0.4360686258
#2 0.1836433242 2.894968537
#3 -0.8356286124 1.229562053
推荐阅读
- r - RandomForest 不接受公式 RHS 上的 log()
- c# - 将 IConfigurationRoot 部分的更改保存到 .net Core 2.2 中的 *.json 文件
- node.js - 无法在 Elasticbeanstark 中部署节点服务器
- css - 如何在应用 minmax 的情况下居中网格内容,以便推送到新行的项目居中对齐?
- java - 以编程方式从 EditText 请求数据
- python - Xgboost 欠拟合
- php - laravel 稍后插入数据
- html - 为什么背景图像不显示?(CSS)
- flutter - Flutter,如何创建嵌入文本的边框?
- selenium - geckodriver 更新到 25 仍显示为 24