与vecor或列表集成为上限R [英] integrate with a vecor or list as upper limit R

查看:75
本文介绍了与vecor或列表集成为上限R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用积分和计算不同的上限.我试图将一个向量和一个列表放入上限参数:

I want to use integrate and calculate for different upper limits. I tried to put a vector and a list into the upper limit parameter:

ul <- as.list(seq(0.001,1,0.001))
integrand <- function(x) {1/((x+1)*sqrt(x))}
test <- integrate(integrand, lower = 0, upper = ul)$val

错误:

Error in is.finite(upper) : default method not implemented for type 'list'

我在for循环中尝试过:

I tried it with a for loop:

ul <- seq(0.001,1,0.001)
for (i in ul){
   test = NULL
   test <- rbind(test, integrate(integrand, lower = 0, upper = S[i])$val[i])
}

错误:Error in if (is.finite(lower) && is.finite(upper)) { : missing value where TRUE/FALSE needed

我可以手动更改上限,然后使用rbind将结果保存在数据帧中,但这将需要很多时间:

I could change the upper limit manually and then save the result with rbind in a data frame, but this would need to much time:

test <-data.frame(test = integrate(integrand, lower = 0, upper = 0.001)$val)
test <- data.frame(rbind(test, integrate(integrand, lower = 0, upper = 0.002)$val)) 

以此类推.

我想我应该使用lappy解决我的问题,但是我对使用它并不熟悉.我该如何解决我的问题?

I guess I should use lappy to solve my problem, but I'm not familiar to using it. How could I solve my problem?

推荐答案

  sapply(ul, function(x) integrate(integrand, lower = 0 , upper = x)$value )

这篇关于与vecor或列表集成为上限R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆