【问题标题】:printing a paragraph in R在 R 中打印一个段落
【发布时间】:2021-12-31 10:15:29
【问题描述】:

我必须从几个简单的数据集中报告一堆基础统计摘要。有没有办法编写一个函数,不仅可以打印出均值、中位数、众数、峰度等,还可以以段落的形式打印出句子,然后我可以将其复制并粘贴到报告中?

理想情况下,我希望 2 个向量 xy 输出类似这样的内容(缩短版):

对于变量 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""

对于倾斜问题,我只需要正确的包。其余的呢?我能做什么?

【问题讨论】:

标签: r


【解决方案1】:

这是一种基于函数strwrap 的方法,它专为打印段落而设计,并为您处理许多格式细节。

以下函数f 接受任意数量的数字向量,并为每个提供的内容打印一个段落。报告的有效位数和一行中的字符数由可选参数digitswidth 控制。

f <- function(..., digits = getOption("digits"), width = getOption("width")) {
  op <- options(digits = digits, width = width)
  on.exit(options(op))
  call <- match.call()
  call[c("digits", "width")] <- NULL
  f1 <- function(x, name) {
    paste0(
      "For variable `", name, "`, ",
      "the mean is ", format(mean(x)), ", while the median is ", format(median(x)), ". ",
      "The highest of the values is ", format(max(x)), " and the minimum is ", format(min(x)), ". ",
      "The standard deviation is ", format(sd(x)), "."
    )
  }
  l <- Map(f1, x = list(...), name = lapply(call[-1L], deparse), USE.NAMES = FALSE)
  s <- do.call(paste, c(l, list(sep = "\n\n")))
  writeLines(strwrap(s))
}

这是一个包含两个数字向量 xy 的测试:

set.seed(181818L)
x <- 1:10
y <- rnorm(10L)
f(x, y, digits = 4L, width = 72L)
For variable `x`, the mean is 5.5, while the median is 5.5. The
highest of the values is 10 and the minimum is 1. The standard
deviation is 3.028.

For variable `y`, the mean is -0.1395, while the median is
0.2025. The highest of the values is 0.9235 and the minimum is
-1.564. The standard deviation is 0.9332.
f(x, y, digits = 6L, width = 60L)
For variable `x`, the mean is 5.5, while the median
is 5.5. The highest of the values is 10 and the
minimum is 1. The standard deviation is 3.02765.

For variable `y`, the mean is -0.139507, while the
median is 0.202508. The highest of the values is
0.923489 and the minimum is -1.56437. The standard
deviation is 0.933158.

【讨论】:

  • 我不明白,但它有效。贾根英尺
【解决方案2】:

你可以这样写一个函数

make_paragraph <- function(v) {
  cat(
    "For Variable ", deparse(v), 
    ", the mean is ", mean(v), 
    ", while the median is ", median(v), 
    ". The highest of the values is ", max(v), 
    ", and the min is ", min(v), 
    ".\n\n", sep=""
  )

  cat(
    "The standard deviation is ", sd(v), 
    ". The set of numbers has skew ", skew(v), 
    ", and kurtosis ", kurtosis(v), 
    ".\n\n", sep=""
  )
}

那么你所要做的就是调用它:

x <- c(1.5, -0.3, 12, 8.5, -100)
make_paragraph(x)

【讨论】:

  • 如果您想更好地控制格式,您可能还需要查看 rmarkdown。
  • 你能告诉我更多吗?我可以使用降价吗?我在降价文件中运行我的代码。你能看看我的编辑,看看可能是什么问题吗?
  • 我同意。在 RStudio 中使用 Rmarkdown。很酷。
猜你喜欢
  • 1970-01-01
  • 2022-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-05
  • 2021-11-09
  • 2019-08-27
相关资源
最近更新 更多