lenght of columns of two objects when more than one column in one object is present

StackOverflow https://stackoverflow.com/questions/21272185

  •  01-10-2022
  •  | 
  •  

Pergunta

I have the following vector and acf function (packages quantmod,XML,zoo)

L<- 1
theurl <- "http://www.iasg.com/groups/group/transtrend/program/transtrend-diversified-   
trend-program-enhanced-risk-usd"
tables <- readHTMLTable(theurl)
n.rows <- unlist(lapply(tables, function(t) dim(t)[1]))
MONTHLYPERFORMANCE<-tables[[which.max(n.rows)]]
z<- as.data.frame(MONTHLYPERFORMANCE);
z$Year<-NULL
z$YTD <- NULL
z$DD <- NULL
z <- z[nrow(z):1,];
s<-as.vector(t(z))
w<-as.numeric(s)
p<-na.omit(w)
x<-(p*0.01*L)
d<-length(na.omit(x)) 
x<- x[1:d]  
#DETREND+DENOISE
s<-detrend(x, tt = 'linear', bp = c())
#AUTOCORRELATION
a<-acf(x)
b<-a[[1]]   
c<-(b[2:length(b)])
posssignificance_level<-qnorm((1+0.95)/2)/sqrt(sum(!is.na(x)))
posssignificance_level
negsignificance_level<- -posssignificance_level
negsignificance_level
poscorr<-which(posssignificance_level<c)
negcorr<-which(negsignificance_level>c)
posautorrelation <- if(length(poscorr) == 0) Lag(s,0) else na.omit(posautorrelation <-  
Lag(s, poscorr))
negautorrelation <- if(length(negcorr) == 0) Lag(s,0) else na.omit(negautorrelation <-  
Lag(s, negcorr))

I then want to make all series resulting from posautorrelation and negautorrelation to take the minimum number of rows either from posautorrelation and negautorrelation by the below expression

shortest <- min(length(posautorrelation), length(negautorrelation))
posautorrelation <- tail(posautorrelation, shortest)
negautorrelation <- tail(negautorrelation, shortest)
tpos <-na.omit(posautorrelation); rownames(tpos)<-NULL
tneg <-na.omit(negautorrelation); rownames(tneg)<-NULL

This works well when I only have one series positive and another negative. However if "posautorrelation" or "negautorrelation" yield more than one column, the shortest function sums the number rows of the different columns handicapping the "shortest" expression from above. Is there a way for "shortest" to consider just one column for posautorelation and the same for negautorrelation.

Foi útil?

Solução

shortest <- min(length(posautorrelation[,1]), length(negautorrelation[,1]))
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top