r功能无法与数据框架正常合作

发布于 2025-02-11 02:02:30 字数 961 浏览 1 评论 0原文

我具有其他人为计算标准偏差而创建的R函数,我想通过CSV文件中的一系列不同值运行它。

以下是函数:

pro_error_carbon <- function(vol,volsd,den,densd,biom,biomsd,nruns=10000, returnsv=NULL) {
  vol <- rnorm(nruns,mean=vol,sd=volsd)
  den <- rnorm(nruns,mean=den,sd=densd) # middle of the road
  biomass <- rnorm(nruns,mean=biom,sd=biomsd) # conifer or angiosperm
  carbt <- vol * den * biomass
  if (!is.null(returnsv)){
    quantile(carbt,probs=returnsv)
  } else {
    c(mean = mean(carbt),sd= sd(carbt))
  }
}

对于单数值,例如:

pro_error_carbon(1.601875,0.133477,0.7008,0.094134,0.5,0.0025)

      mean         sd 
0.55987958 0.08952082 

但是,当我尝试使用CSV文件中的值时,我会得到:

pro_error_carbon(leafoff$Volumem3,leafoff$VolumeSDkg,leafoff$Wood.density,leafoff$WoodSD,0.5,0.0025,nruns=10000, returnsv=NULL)

##I get this:

  mean       sd 
1.599976 1.717951 

我希望能够以不同的值运行它并获得其个人均值&amp;标准偏差。运行循环会为此工作吗?我该怎么做?

I have an R function that someone else created to calculate standard deviation and I wanted to run it through a series of different values in a csv file.

Here is the function:

pro_error_carbon <- function(vol,volsd,den,densd,biom,biomsd,nruns=10000, returnsv=NULL) {
  vol <- rnorm(nruns,mean=vol,sd=volsd)
  den <- rnorm(nruns,mean=den,sd=densd) # middle of the road
  biomass <- rnorm(nruns,mean=biom,sd=biomsd) # conifer or angiosperm
  carbt <- vol * den * biomass
  if (!is.null(returnsv)){
    quantile(carbt,probs=returnsv)
  } else {
    c(mean = mean(carbt),sd= sd(carbt))
  }
}

It works fine for singular values EG:

pro_error_carbon(1.601875,0.133477,0.7008,0.094134,0.5,0.0025)

      mean         sd 
0.55987958 0.08952082 

But when I try using values from a csv file I get:

pro_error_carbon(leafoff$Volumem3,leafoff$VolumeSDkg,leafoff$Wood.density,leafoff$WoodSD,0.5,0.0025,nruns=10000, returnsv=NULL)

##I get this:

  mean       sd 
1.599976 1.717951 

I want to be able to run this for different values and get their individual means & standard deviations. Would running a loop work for this and how would I do something like that?

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

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

发布评论

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

评论(1

树深时见影 2025-02-18 02:02:30

您可以使用purrr的pmap_dfr通过各种输入参数的列表进行映射,并输出earne> Meand s和sd s的dataframe,例如:

library(tidyverse)

pro_error_carbon <- function(vol, volsd, den, densd, biom, biomsd, nruns = 10000, returnsv = NULL) {
  vol <- rnorm(nruns, mean = vol, sd = volsd)
  den <- rnorm(nruns, mean = den, sd = densd) # middle of the road
  biomass <- rnorm(nruns, mean = biom, sd = biomsd) # conifer or angiosperm
  carbt <- vol * den * biomass
  if (!is.null(returnsv)) {
    quantile(carbt, probs = returnsv)
  } else {
    c(mean = mean(carbt), sd = sd(carbt))
  }
}

# 4 Sample Inputs for the Function
list <- tribble(
  ~vol, ~volsd, ~den, ~densd, ~biom, ~biomsd,
  1.601875, 0.133477, 0.7008, 0.094134, 0.5, 0.0025,
  1.601874, 0.133476, 0.7007, 0.094133, 0.4, 0.0024,
  1.601873, 0.133475, 0.7006, 0.094132, 0.3, 0.0023,
  1.601872, 0.133474, 0.7005, 0.094131, 0.2, 0.0022,
) |> 
  as.list()

# Map through the 4 inputs
pmap_dfr(list, pro_error_carbon)

#> # A tibble: 4 × 2
#>    mean     sd
#>   <dbl>  <dbl>
#> 1 0.561 0.0883
#> 2 0.449 0.0711
#> 3 0.336 0.0533
#> 4 0.225 0.0355

在2022-06-27创建的 reprex package (v2.0.1)

You could use purrr's pmap_dfr to map through a list of the various input arguments and output a dataframe of the means and sds like this:

library(tidyverse)

pro_error_carbon <- function(vol, volsd, den, densd, biom, biomsd, nruns = 10000, returnsv = NULL) {
  vol <- rnorm(nruns, mean = vol, sd = volsd)
  den <- rnorm(nruns, mean = den, sd = densd) # middle of the road
  biomass <- rnorm(nruns, mean = biom, sd = biomsd) # conifer or angiosperm
  carbt <- vol * den * biomass
  if (!is.null(returnsv)) {
    quantile(carbt, probs = returnsv)
  } else {
    c(mean = mean(carbt), sd = sd(carbt))
  }
}

# 4 Sample Inputs for the Function
list <- tribble(
  ~vol, ~volsd, ~den, ~densd, ~biom, ~biomsd,
  1.601875, 0.133477, 0.7008, 0.094134, 0.5, 0.0025,
  1.601874, 0.133476, 0.7007, 0.094133, 0.4, 0.0024,
  1.601873, 0.133475, 0.7006, 0.094132, 0.3, 0.0023,
  1.601872, 0.133474, 0.7005, 0.094131, 0.2, 0.0022,
) |> 
  as.list()

# Map through the 4 inputs
pmap_dfr(list, pro_error_carbon)

#> # A tibble: 4 × 2
#>    mean     sd
#>   <dbl>  <dbl>
#> 1 0.561 0.0883
#> 2 0.449 0.0711
#> 3 0.336 0.0533
#> 4 0.225 0.0355

Created on 2022-06-27 by the reprex package (v2.0.1)

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