舍入汇总函数导致错误

时间:2016-05-08 15:42:47

标签: r rounding apply

尝试使用from flask import Flask, jsonify from jinja2 import Template import math from bokeh.plotting import figure from bokeh.models import AjaxDataSource from bokeh.embed import components from bokeh.resources import INLINE from bokeh.util.string import encode_utf8 app = Flask(__name__) x, y = 0, 0 @app.route("/data", methods=['POST']) def get_x(): global x, y x = x + 0.1 y = math.sin(x) return jsonify(x=[x], y=[y]) template = Template('''<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Streaming Example</title> {{ js_resources }} {{ css_resources }} </head> <body> {{ plot_div }} {{ plot_script }} </body> </html> ''') @app.route("/") def simple(): streaming=True source = AjaxDataSource(data_url="http://localhost:5000/data", polling_interval=1000, mode='append') source.data = dict(x=[], y=[]) fig = figure(title="Streaming Example") fig.line( 'x', 'y', source=source) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(fig, INLINE) html = template.render( plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources ) return encode_utf8(html) app.run(debug=True) 包中的summarize输出摘要统计信息。 希望统计数据四舍五入为2位小数。我收到错误消息时 在rockchalk上使用round

summarize

有什么想法吗?我可以围绕library(rockchalk) M1 <- structure(c(0.18, 0.2, 0.24, 0.35, -0.22, -0.17, 0.28, -0.28, -0.14, 0.03, 0.87, -0.2, 0.06, -0.1, -0.72, 0.18, 0.01, 0.31, -0.36, 0.61, -0.16, -0.07, -0.13, 0.01, -0.09, 0.26, -0.14, 0.08, -0.62, -0.2, 0.3, -0.21, -0.11, 0.05, 0.06, -0.28, -0.27, 0.17, 0.42, -0.05, -0.15, 0.05, -0.07, -0.22, -0.34, 0.16, 0.34, 0.1, -0.12, 0.24, 0.45, 0.37, 0.61, 0.9, -0.25, 0.02), .Dim = c(56L, 1L)) #This works round(apply(M1, 2, mean),2) #This works summaryround <- function(x) {round(summary(x),2)} apply(M1, 2, summaryround) #This gives error "non-numeric argument" round(apply(M1, 2, summarize),2) #Thought this would work but also gives error "non-numeric argument" summarizeround <- function(x) {round(summarize(x),2)} apply(M1, 2, summarizeround) 的输出,但想要使用summary 如果可能的话,因为我喜欢在同一个打印输出中获得峰度和偏度的输出(当然,可以创建我自己的函数,结合summarizesummary以及我想要的任何内容,而不是如果可以避免的话。 / p>

编辑:应该提到在大型数据框架上实际运行它;把它变成了一个1列矩阵,因为我认为这会使可重现的例子变得更简单。

2 个答案:

答案 0 :(得分:2)

您只需从numerics结果中提取summarize字段即可。此外,如果您要汇总多列,我更愿意使用lapply来保留结果的rownames并使用do.call(bind,...)

summarizeround <- function(x) {round(summarize(x)$numerics,2)} 
summaryDf <- do.call(cbind, lapply(as.data.frame(M1), summarizeround))

             x
0%       -0.72
25%      -0.16
50%       0.02
75%       0.24
100%      0.90
mean      0.04
sd        0.32
var       0.10
skewness  0.45
kurtosis  0.56
NA's      0.00
N        56.00

答案 1 :(得分:0)

?rockchalk :: summarize说这个论点是一个数据框架。因此,使M1成为数据框

M1<-as.data.frame(M1)
summarize(M1)

$numerics
              V1
0%       -0.7200
25%      -0.1625
50%       0.0150
75%       0.2400
100%      0.9000
mean      0.0400
sd        0.3152
var       0.0993
skewness  0.4485
kurtosis  0.5626
NA's      0.0000
N        56.0000

$factors
NULL

并获得四舍五入

> round(summarize(M1)[[1]],2)
            V1
0%       -0.72
25%      -0.16
50%       0.02
75%       0.24
100%      0.90
mean      0.04
sd        0.32
var       0.10
skewness  0.45
kurtosis  0.56
NA's      0.00
N        56.00