1
得到的返回值我想用下面R
腳本Python
:rpy2:如何從調用一個R函數
> library(bfast)
> apple <- read.csv("/Users/nskalis/Downloads/R/apple.csv", sep = ";", header=TRUE)
> data = apple
# data$in_bps: is vector of double numbers
> data.ts <- ts(data$in_bps, frequency=1)
> data.fit <- bfast(data.ts, h=0.1, season="none", max.iter=1)
> data.fit$output[[1]]$Tt
> data.fit$output[[1]]$Vt.bp
> data.fit$output[[1]]$ci.Vt
> data.fit$output[[1]]$ci.Vt$confint
因此我使用rpy2
,我也做了以下內容:
from rpy2.robjects.packages import importr
import rpy2.robjects as robjects
importr("bfast")
data = range(1,100)
data = robjects.FloatVector(data)
data = robjects.r.ts(data, frequency=1)
x = robjects.r.bfast(data, h=0.1, season="none", max_iter=1)
結果變量x
等於
In [42]: x
Out[42]:
R object with classes: ('bfast',) mapped to:
<ListVector - Python:0x7f234f7ad6c8/R:0x76a2d60>
[Float..., ListV..., ListV..., ..., Float..., BoolV..., ListV...]
Yt: <class 'rpy2.robjects.vectors.FloatVector'>
R object with classes: ('ts',) mapped to:
<FloatVector - Python:0x7f234fd22dc8/R:0x7605740>
[1.000000, 2.000000, 3.000000, ..., 97.000000, 98.000000, 99.000000]
R object with classes: ('bfast',) mapped to:
<ListVector - Python:0x7f234f7ad6c8/R:0x76a2d60>
[Float..., ListV..., ListV..., ..., Float..., BoolV..., ListV...]
R object with classes: ('bfast',) mapped to:
<ListVector - Python:0x7f234f7ad6c8/R:0x76a2d60>
[Float..., ListV..., ListV..., ..., Float..., BoolV..., ListV...]
...
Yt: <class 'rpy2.robjects.vectors.FloatVector'>
R object with classes: ('numeric',) mapped to:
<FloatVector - Python:0x7f234c053388/R:0x586b668>
[0.000000]
output: <class 'rpy2.robjects.vectors.BoolVector'>
R object with classes: ('logical',) mapped to:
<BoolVector - Python:0x7f234c04eac8/R:0x57ee518>
[NA]
R object with classes: ('bfast',) mapped to:
<ListVector - Python:0x7f234f7ad6c8/R:0x76a2d60>
[Float..., ListV..., ListV..., ..., Float..., BoolV..., ListV...]
您能否告知如何獲取變量data.fit$output[[1]]$Vt.bp
?附:
PS:這是我第一次與rpy2
,所以請隨時告訴我,如果我已經做了任何錯誤。
我想有對結果沒有引用,如一切是一個向量和所期望的數據分別位於:'列表(x [1] [0] [4] [0] )' – iamsterdam
你有沒有看過文檔? http://rpy2.readthedocs.io/en/version_2.8.x/vector.html#extracting-items – lgautier