是否可以在R中具有多个变量的函数

发布于 2025-01-22 17:56:09 字数 475 浏览 2 评论 0原文

我正在研究一个要测试三个时间序列的正态性的功能。我创建了以下功能:

par(mfrow =  c(3,2))
graphicalnormality = function(x){
  plotNormalHistogram(x)
  normstats = c(mean(x), median(x), quantile(x, c(0.1, 0.9)))
  abline(v = normstats, col = 'red', lwd = 2)
  qqnorm(x)
  qqline(x)
}
graphicalnormality(OBX)
graphicalnormality(DNB)
graphicalnormality(DNO)

这是一个非常简单的功能,它可以使用它的方式,但是由于我有点挑剔,而且因为我不确定它与rmarkdown的效果如何,所以我不想拥有要运行三次功能以获取我的所有三个测试的绘图。因此,我的问题是,是否可以一次使用所有三个数据集运行该功能?

I am working on a function where I want to test normality of three time series. I have created the following function:

par(mfrow =  c(3,2))
graphicalnormality = function(x){
  plotNormalHistogram(x)
  normstats = c(mean(x), median(x), quantile(x, c(0.1, 0.9)))
  abline(v = normstats, col = 'red', lwd = 2)
  qqnorm(x)
  qqline(x)
}
graphicalnormality(OBX)
graphicalnormality(DNB)
graphicalnormality(DNO)

It is a very simple function and it works how I want it to, but since I am just a little bit picky and because I am unsure how well it works with RMarkdown, I don't want to have to run the function three times to get the plot for all three of my tests. So my question is, is it possible to get the function to run for all three data sets in one go?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耀眼的星火 2025-01-29 17:56:09

您可以修改您的功能以获取任何类似的向量:

graphicalnormality = function(...) {
  dfs <- lapply(as.list(match.call())[-1], eval)
  for(x in dfs) {
    rcompanion::plotNormalHistogram(x)
    normstats = c(mean(x), median(x), quantile(x, c(0.1, 0.9)))
    abline(v = normstats, col = 'red', lwd = 2)
    qqnorm(x)
    qqline(x)
  }
}

因此您可以做:

par(mfrow =  c(3,2))
graphicalnormality(OBX, DNB, DNC)

”在此处输入图像描述”


data

OBX <- rnorm(100)
DNB <- rnorm(100)
DNC <- rnorm(100)

You can modify your function to take any number of vectors like this:

graphicalnormality = function(...) {
  dfs <- lapply(as.list(match.call())[-1], eval)
  for(x in dfs) {
    rcompanion::plotNormalHistogram(x)
    normstats = c(mean(x), median(x), quantile(x, c(0.1, 0.9)))
    abline(v = normstats, col = 'red', lwd = 2)
    qqnorm(x)
    qqline(x)
  }
}

So you can do:

par(mfrow =  c(3,2))
graphicalnormality(OBX, DNB, DNC)

enter image description here


Data

OBX <- rnorm(100)
DNB <- rnorm(100)
DNC <- rnorm(100)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文