Hi, R newb here. I've coded a function that inputs N dimensional array(s) [or class=numeric if it's dim=1] of coefficients and tstats, where dim(coef_matrix)=dim(tstat_matrix), it will then output a same dimension matrix of coefficients pasted to tstats in brackets pasted to significance stars.
If I go straight to the code INSIDE the function, it works 100% as it's supposed to. The output matrix has the correct dimensions. However, if I actually use the function, it'll do something like this. fun.starpaste(3D matrix tstats,3D matrix coefficients) ::: CORRECT OUTPUT, 3 dimensional array. fun.starpaste(1D numeric tstats, 1D numeric coefficients) ::: WRONG output, output is a 3 dimensional array. And it'll keep returning a 3 dimensional array (it'll be the correct 1 dimensional vector repeated numerous times to fill 3 dimensions). Then I'll copy and paste the function code again and again into the R console, and it'll reset the slate: fun.starpaste(1D numeric, 1D numeric) ::: CORRECT output, 1 dimensional vector. fun.starpaste(2D or 3D matrix, 2D or 3D matrix) ::: WRONG output, gives me a massive 1 dimensional vector. What the heck?? The function code is below. #---------------------------------------------------------------------------------------------- fun.starpaste <- function(t,co=NULL,tails=2,sig=c(0.9,0.95,0.99)) #handles the pasting of #coef(tstat)significance_stars { if(length(dim(t))>1)t3<-t else fake1<-1 t<-matrix(t) if(is.null(co)==TRUE)fake<-1 else co<-matrix(co) if(exists(as.character(substitute(fake1)))==FALSE)fake<-1 else t3<-t tstat<-as.numeric(t);tstat[is.na(tstat)]<-0 sig.l<-length(sig) stars<-rawToChar(charToRaw("*********************************************"),multiple=T) MISC1<-0 for(x in 1:length(sig)){ if(tails==1)cutoff<-qt(sig[x],Inf) else if(tails==2)cutoff<-qt(sig[x]+0.5*(1-sig[x]),Inf) else fake<-1 MISC1<-cbind(MISC1,cutoff) } cutoff<-MISC1[-1] for(i in 1:length(tstat)){ if(abs(as.numeric(tstat[i]))<=cutoff[1])tstat[i]<-c("") else fake<-1 } for(i in 1:length(tstat)){ for(x in 1:(sig.l-1)){ if(sum(apply(matrix(1:(sig.l+1),nrow=1),c(1,2),function(N){ if(tstat[i]==cbind(c(""),c("*"),c("**"),c("***"),c("****"),c("*****"),c("******"))[N])1 else 0 } ))==0 && abs(as.numeric(tstat[i]))>cutoff[x] && abs(as.numeric(tstat[i]))<cutoff[x+1]) tstat[i]<-paste(stars[1:x],collapse="",sep="") else fake<-1 if(sum(apply(matrix(1:(sig.l+1),nrow=1),c(1,2),function(N){ if(tstat[i]==cbind(c(""),c("*"),c("**"),c("***"),c("****"),c("*****"),c("******"))[N])1 else 0 } ))==0 && abs(as.numeric(tstat[i]))>=cutoff[sig.l]) tstat[i]<-paste(stars[1:sig.l],collapse="",sep="") else fake<-1 }} options(warn=-1) a<-as.numeric(tstat);b<-a[!is.na(a)] if(sum(b)>0){a<-cbind(1:length(tstat),a);a<-a[!is.na(a[,2]),1];tstat[a]<-c("ERROR")} else fake<-1 data2<-array(tstat,dim=dim(t)) dimnames(data2)<-dimnames(t) t2<-format(round(t,tround),nsmall=tround,trim=T) if(is.null(co)==FALSE){ co<-format(round(co,coefround),nsmall=coefround,trim=T) t<-paste(co,c("("),t2,c(")"),data2,sep="") t<-array(t,dim=c(length(t),1)) dimnames(t)<-dimnames(data2) } else {t<-data2} if(length(dim(t3))>1 && length(dim(t3)[dim(t3)==1])==0)t<-array(t,dim=dim(t3)) else fake<-1 return(t) } ----- ---- Isaac Research Assistant Quantitative Finance Faculty, UTS -- View this message in context: http://r.789695.n4.nabble.com/Very-strange-function-behaviour-tp4247729p4247729.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.