r功能无法与数据框架正常合作
我具有其他人为计算标准偏差而创建的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用purrr的
pmap_dfr
通过各种输入参数的列表进行映射,并输出earne> Meand
s和sd
s的dataframe,例如:在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 themean
s andsd
s like this:Created on 2022-06-27 by the reprex package (v2.0.1)