2014-01-29 1 views
2

시간 계열 개체를 받아들이는 함수를 작성했습니다. 입력 데이터를 xts 형식으로 강제 변환하려고하지만, 그렇게하면 deparse (대체)가 문제가됩니다. , 그것은 객체의 이름을 반환해야한다.개체를 xts로 강제 변환 할 때 개체 이름을 가져올 수 없습니다.

library(xts) 
data(sample_matrix) 
matrixObj=sample_matrix 

myFunc=function(data){ 
    print(deparse(substitute(data))) 
} 

myFunc(matrixObj) 

[1] "matrixObj"#이 내가 원하는 무엇인가 :

코드는 오류를 재현합니다.

myFunc2=function(data){ 
    data=as.xts(data)#add this line to coerce data into xts format 
    print(deparse(substitute(data))) 
} 

myFunc2(matrixObj) 

[1] "의 구조 (c (50.0397819115463, 50.2304961977954, 50.420955209067"
[2] "50.3734680543285, 50.2443255196795, 50.1321122972067, 50.0355467742705"
[3] "49.9948860954217, 49.9122834457642, 49.8852887132391, 50.2125821224916 "
[4]"50.3238453485025, 50.4635862266585, 50.6172355897254, 50.620241173435 "
[5]"50.7414981135498, 50.4805101188755, 50.4138129108681, 50.3532310036568 "
[6]"50.1618813949374, 50.3600836896748, 50.0396626712588, 50.1095259574076 "
[7] "50.2073807897632, 50.1600826681477, 50.0604060861532, 49.9658575219043" ... 생략 ...

당신 때문이 아니라 개체의 유형, 함수에서 개체를 수정하고 때문입니다

답변

1

. 원하는 결과는 또한

library(xts) 
data(sample_matrix) 
matrixObj=sample_matrix 

xtsObj=as.xts(sample_matrix) 

myFunc=function(data){ 

    print(deparse(substitute(data))) 
} 

myFunc(xtsObj) 
#[1] "matrixObj" 

MYFUNC로의 XTS를 제공 전달, 어느 XTS에 대해 다른 변수 이름을 사용하여 함수에서 개체 또는 변수 이름을받은 후 강제 변환을 수행하여 작동합니다 :

library(xts) 
data(sample_matrix) 
matrixObj=sample_matrix 

#use a different variable name for the xts object 
myFunc3=function(data){ 
    xtsdata=as.xts(data) 
    print(deparse(substitute(data))) 

} 

myFunc3(matrixObj) 
#[1] "matrixObj" 

#get the name before doing the coercion 
myFunc4=function(data){ 
    print(deparse(substitute(data))) 
    data=as.xts(data) 

} 

myFunc4(matrixObj) 
#[1] "matrixObj"