【发布时间】:2021-12-31 10:15:29
【问题描述】:
我必须从几个简单的数据集中报告一堆基础统计摘要。有没有办法编写一个函数,不仅可以打印出均值、中位数、众数、峰度等,还可以以段落的形式打印出句子,然后我可以将其复制并粘贴到报告中?
理想情况下,我希望 2 个向量 x 和 y 输出类似这样的内容(缩短版):
对于变量 x,均值是均值 (x),而中值是中值 (x)。最大值为 max(x),最小值为 min(x)。
标准差为 sd(x)。这组数字有skew skew(x)和kurtosis kurtosis()。
对于变量 y,均值是均值 (y),而中值是中值 (y)。最大值为 max(y),最小值为 min(y)。
标准差是 sd(y)。数字集合有skew skew(y), kurtosis kurtosis()
编辑:
使用用户的提示后,我做了这样的事情:
make_paragraph <- function(v, w) {
cat(
"For vector a, values range from a low of ",min(v),
"to a high of", max(v),
".The mean time is ", mean(v),
", while the median is ", median(v),
". We have a variance of", var(v),
"and thus a standard deviation of", sd(v),
". The Inter Quartile Range is ", IQR(v),
". A time value of",quantile(v,0.25),
"marks the 25th percentile",quantile(v, .5),
"marks the 50th percentile,",quantile(v, .75),
"marks the 75th percentile. The kurtosis
is",kurtosis(v),
"indication that time is.... The skewness number is",skew(v),
"indicating that the distribution is ...."
".\n\n", sep=""
)
cat(
"For women, values range from a low of ",min(w),
"to a high of", max(w),
".The mean time is ", mean(w),
", while the median is ", median(w),
". We have a variance of", var(w),
"and thus a standard deviation of", sd(w),
". The Inter Quartile Range is ", IQR(w),
". A time value of",quantile(v,0.25),
"marks the 25th percentile",quantile(v,0.5),
"marks the 50th percentile,",quantile(v,.75),
"marks the 75th percentile. The kurtosis is", kurtosis(v),
"indication that winning time is.... The skewness number is"
,skew(w),
"indicating that the distribution is ...."
".\n\n", sep=""
)
}
我刚刚更改了一些内容,但我收到了一条错误提示
[1] "y"
For Variable c(1.5, -0.3, 12, 8.5, -100), the mean is -15.66, while the median is 1.5. The highest of the values is 12, and the min is -100.
Error in skew(v) : could not find function "skew"
[1] "For variable %f"
Error in print.default("The mean is, ", mean(x)) : invalid printing digits -15
Error: unexpected string constant in:
"
".\n\n""
对于倾斜问题,我只需要正确的包。其余的呢?我能做什么?
【问题讨论】:
-
这里讨论了如何混合打印文本和变量值:stackoverflow.com/questions/32241806/…
标签: r