我正在尝试使用python调用r中使用的ccf函数。
from rpy2.robjects.packages import SignatureTranslatedAnonymousPackage
string = """
cc <- function(x,y) {
ccf(x,y)
}
"""
powerpack = SignatureTranslatedAnonymousPackage(string, "powerpack")
通话:
import rpy2.robjects as ro
x = [1,2,3,4,5,5]
y = [5,6,7,8,8,9]
x = ro.Vector(tuple(x))
y = ro.Vector(tuple(y))
print (powerpack.cc(x,y))
错误:
RRuntimeError: Error in x[, (1 + cs[i]):cs[i + 1]] <- xx :
incorrect number of subscripts on matrix
关于如何纠正此问题的任何建议都很好。
答案 0 :(得分:0)
尝试一下:
from rpy2.robjects.packages import SignatureTranslatedAnonymousPackage
import rpy2.robjects as ro
string = """
cc <- function(x,y) {
xx <- unlist(x, recursive=FALSE)
yy <- unlist(y, recursive=FALSE)
ccf(xx, yy)
}
"""
powerpack = SignatureTranslatedAnonymousPackage(string, "powerpack")
x = [1,2,3,4,5,5]
y = [5,6,7,8,8,9]
x = ro.Vector(x)
y = ro.Vector(y)
print (powerpack.cc(x,y))
说明:
ro.Vector(tuple(x))
,则会收到其他错误。 NotImplementedError: Conversion 'py2ri' not defined for objects of type ''
至少对于我的python版本(3.7.0)。所以这可能不是您想要的。
unlist()
才能应用ccf()
。请注意,在这种情况下,您实际上确实会收到报告的错误。