integrate with a vecor or list as upper limit R

别说谁变了你拦得住时间么 提交于 2019-12-12 06:18:03

问题


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:

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

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: Error in if (is.finite(lower) && is.finite(upper)) { : missing value where TRUE/FALSE needed

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)) 

and so on.

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


回答1:


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


来源:https://stackoverflow.com/questions/43917440/integrate-with-a-vecor-or-list-as-upper-limit-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!