Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign result of integrate in R to a numeric variable?

R example:

k=6
f<-function(s){s^(k-1)*exp(-s)}
integrate(f,0,Inf)

The output of integrate is the string:

120 with absolute error < 7.3e-05

I want to assign the first value in the string (120, the integral) to a variable. How to do that?

like image 324
user1325206 Avatar asked Nov 18 '15 18:11

user1325206


1 Answers

The result of integrate is a list:

> temp <- integrate(f,0,Inf)
> temp
120 with absolute error < 7.3e-05
> str(temp)
List of 5
 $ value       : num 120
 $ abs.error   : num 7.34e-05
 $ subdivisions: int 5
 $ message     : chr "OK"
 $ call        : language integrate(f = f, lower = 0, upper = Inf)
 - attr(*, "class")= chr "integrate"

You can access elements by name:

> temp$value
[1] 120

... or by index:

> temp[[2]]
[1] 7.335833e-05
like image 120
A5C1D2H2I1M1N2O1R2T1 Avatar answered Nov 15 '22 00:11

A5C1D2H2I1M1N2O1R2T1