par1 <- '1 2 3 4 5' docor <- function(x,y,method) { r <- cor.test(x,y,method=method) paste(round(r$estimate,3),' (',round(r$p.value,3),')',sep='') } x <- t(x) nx <- length(x[,1]) cx <- length(x[1,]) mymedian <- median(as.numeric(strsplit(par1,' ')[[1]])) myresult <- array(NA, dim = c(cx,7)) rownames(myresult) <- paste('Q',1:cx,sep='') colnames(myresult) <- c('mean','Sum of positives (Ps)','Sum of negatives (Ns)', '(Ps-Ns)/(Ps+Ns)', 'Count of positives (Pc)', 'Count of negatives (Nc)', '(Pc-Nc)/(Pc+Nc)') for (i in 1:cx) { spos <- 0 sneg <- 0 cpos <- 0 cneg <- 0 for (j in 1:nx) { if (!is.na(x[j,i])) { myx <- as.numeric(x[j,i]) - mymedian if (myx > 0) { spos = spos + myx cpos = cpos + 1 } if (myx < 0) { sneg = sneg + abs(myx) cneg = cneg + 1 } } } myresult[i,1] <- round(mean(as.numeric(x[,i]),na.rm=T)-mymedian,2) myresult[i,2] <- spos myresult[i,3] <- sneg myresult[i,4] <- round((spos - sneg) / (spos + sneg),2) myresult[i,5] <- cpos myresult[i,6] <- cneg myresult[i,7] <- round((cpos - cneg) / (cpos + cneg),2) } print(myresult) load(file='createtable') a<-table.start() a<-table.row.start(a) a<-table.element(a,'Summary of survey scores (median of Likert score was subtracted)',8,TRUE) a<-table.row.end(a) a<-table.row.start(a) a<-table.element(a,'Question',header=TRUE) for (i in 1:7) { a<-table.element(a,colnames(myresult)[i],header=TRUE) } a<-table.row.end(a) for (i in 1:cx) { a<-table.row.start(a) a<-table.element(a,i,header=TRUE) for (j in 1:7) { a<-table.element(a,myresult[i,j],align='right') } a<-table.row.end(a) } a<-table.end(a) table.save(a,file='mytable.tab') a<-table.start() a<-table.row.start(a) a<-table.element(a,'Pearson correlations of survey scores (and p-values)',4,TRUE) a<-table.row.end(a) a<-table.row.start(a) a<-table.element(a,'',header=TRUE) a<-table.element(a,'mean',header=TRUE) a<-table.element(a,'(Ps-Ns)/(Ps+Ns)',header=TRUE) a<-table.element(a,'(Pc-Nc)/(Pc+Nc)',header=TRUE) a<-table.row.end(a) a<-table.row.start(a) a<-table.element(a,'mean',header=TRUE) a<-table.element(a,docor(myresult[,1],myresult[,1],method='pearson'),align='right') a<-table.element(a,docor(myresult[,1],myresult[,4],method='pearson'),align='right') a<-table.element(a,docor(myresult[,1],myresult[,7],method='pearson'),align='right') a<-table.row.end(a) a<-table.row.start(a) a<-table.element(a,'(Ps-Ns)/(Ps+Ns)',header=TRUE) a<-table.element(a,docor(myresult[,4],myresult[,1],method='pearson'),align='right') a<-table.element(a,docor(myresult[,4],myresult[,4],method='pearson'),align='right') a<-table.element(a,docor(myresult[,4],myresult[,7],method='pearson'),align='right') a<-table.row.end(a) a<-table.row.start(a) a<-table.element(a,'(Pc-Nc)/(Pc+Nc)',header=TRUE) a<-table.element(a,docor(myresult[,7],myresult[,1],method='pearson'),align='right') a<-table.element(a,docor(myresult[,7],myresult[,4],method='pearson'),align='right') a<-table.element(a,docor(myresult[,7],myresult[,7],method='pearson'),align='right') a<-table.row.end(a) a<-table.end(a) table.save(a,file='mytable1.tab') a<-table.start() a<-table.row.start(a) a<-table.element(a,'Kendall tau rank correlations of survey scores (and p-values)',4,TRUE) a<-table.row.end(a) a<-table.row.start(a) a<-table.element(a,'',header=TRUE) a<-table.element(a,'mean',header=TRUE) a<-table.element(a,'(Ps-Ns)/(Ps+Ns)',header=TRUE) a<-table.element(a,'(Pc-Nc)/(Pc+Nc)',header=TRUE) a<-table.row.end(a) a<-table.row.start(a) a<-table.element(a,'mean',header=TRUE) a<-table.element(a,docor(myresult[,1],myresult[,1],method='kendall'),align='right') a<-table.element(a,docor(myresult[,1],myresult[,4],method='kendall'),align='right') a<-table.element(a,docor(myresult[,1],myresult[,7],method='kendall'),align='right') a<-table.row.end(a) a<-table.row.start(a) a<-table.element(a,'(Ps-Ns)/(Ps+Ns)',header=TRUE) a<-table.element(a,docor(myresult[,4],myresult[,1],method='kendall'),align='right') a<-table.element(a,docor(myresult[,4],myresult[,4],method='kendall'),align='right') a<-table.element(a,docor(myresult[,4],myresult[,7],method='kendall'),align='right') a<-table.row.end(a) a<-table.row.start(a) a<-table.element(a,'(Pc-Nc)/(Pc+Nc)',header=TRUE) a<-table.element(a,docor(myresult[,7],myresult[,1],method='kendall'),align='right') a<-table.element(a,docor(myresult[,7],myresult[,4],method='kendall'),align='right') a<-table.element(a,docor(myresult[,7],myresult[,7],method='kendall'),align='right') a<-table.row.end(a) a<-table.end(a) table.save(a,file='mytable2.tab')
|