[R] printing
Heberto Ghezzo
Heberto at meakins.lan.mcgill.ca
Thu Dec 7 14:14:02 CET 2000
Hello
I need some help in printing some means crossclasified
by 2 or 3 other variables. I wrote 2 simple functions, xt and
xtabmean.
Now xt has an unclear print out and adds a NULL at the end of each
line that I do not know where it comes from.
xtabmean has a neater print out but...
> j<-rep(1:4,9)
> i<-rep(1:3,12)
> x<-rnorm(36)
> xt(x,i,j)
1 1 -0.5675582 1.224832 3NULL
1 2 0.1040071 1.411071 3NULL
1 3 0.1230299 0.7261805 3NULL
1 4 -0.3992837 0.7206155 3NULL
2 1 -1.061823 0.8568403 3NULL
2 2 0.5460621 0.9337556 3NULL
2 3 0.06103977 0.8620043 3NULL
2 4 -0.4616834 1.071529 3NULL
3 1 0.3307771 0.7646795 3NULL
3 2 0.2904226 0.6446345 3NULL
3 3 0.1830588 0.7805733 3NULL
3 4 0.3428314 1.164575 3NULL
> xtabmean(x,i,j)
, , 1
1 2 3
Mean -0.57 -1.06 0.33
S.Dev 1.22 0.86 0.76
n 3.00 3.00 3.00
, , 2
1 2 3
Mean 0.10 0.55 0.29
S.Dev 1.41 0.93 0.64
n 3.00 3.00 3.00
, , 3
1 2 3
Mean 0.12 0.06 0.18
S.Dev 0.73 0.86 0.78
n 3.00 3.00 3.00
, , 4
1 2 3
Mean -0.40 -0.46 0.34
S.Dev 0.72 1.07 1.16
n 3.00 3.00 3.00
Now if I create some NAs so a cell is empty
> i[i==2 & j==3] <- NA
> xtabmean(x,i,j)
, , 1
1 2 3
Mean -0.57 -1.06 0.33
S.Dev 1.22 0.86 0.76
n 3.00 3.00 3.00
, , 2
1 2 3
Mean 0.10 0.55 0.29
S.Dev 1.41 0.93 0.64
n 3.00 3.00 3.00
, , 3
1 2 3
Mean 0.12 0.18 -0.40
S.Dev 0.73 0.78 0.72
n 3.00 3.00 3.00
, , 4
1 2 3
Mean -0.46 0.34 -0.57
S.Dev 1.07 1.16 1.22
n 3.00 3.00 3.00
>
as you can see xtabmean jumps over the cell with zero obs and
prints the rest out of sinc. The last obs 3,4 is the same as the first
> xt(x,i,j)
1 1 -0.5675582 1.224832 3NULL
1 2 0.1040071 1.411071 3NULL
1 3 0.1230299 0.7261805 3NULL
1 4 -0.3992837 0.7206155 3NULL
2 1 -1.061823 0.8568403 3NULL
2 2 0.5460621 0.9337556 3NULL
2 3 NULL
2 4 -0.4616834 1.071529 3NULL
3 1 0.3307771 0.7646795 3NULL
3 2 0.2904226 0.6446345 3NULL
3 3 0.1830588 0.7805733 3NULL
3 4 0.3428314 1.164575 3NULL
>
xt though computes properly the means but I would like to
format the output as that from xtabmean, and get rid of those NULL.
Can anybody offer some help?
My version of the functios are:
xt <- function(v,b,c) {
a<-by(v,list(c,b),fun)
k<-0
ab <- b[!is.na(b)]
ac <- c[!is.na(c)]
for(i in 1:length(unique(ab)))
for(j in 1:length(unique(ac)))
{k<- k+1; print(cat(i," ",j," ",a[[k]][1],a[[k]][2],a[[k]][3]))}
}
xtabmean <- function(y,i,j=1,k=1)
{
fun <- function(x)
c(mean(x,na.rm=T),sd(x,na.rm=T),length(x))
#
ii<- i[!is.na(i)]
jj <- j[!is.na(j)]
kk <- k[!is.na(k)]
#
li <- length(unique(ii))
lj <- length(unique(jj))
lk <- length(unique(kk))
#
if((lj>1) && (lk>1)){
t <- tapply(y,list(i,j,k),fun)
a <- array(c(t,recursive=T),c(3,li,lj,lk))
dimnames(a) <- list(c("Mean","S.Dev","n"),1:li,1:lj,1:lk)
}
if(lj>1 && lk==1){
t <- tapply(y,list(i,j),fun)
a <- array(c(t,recursive=T),c(3,li,lj))
dimnames(a) <- list(c("Mean","S.Dev","n"),1:li,1:lj)
}
if(lj==1){
t <- tapply(y,i,fun)
a <- array(c(t,recursive=T),c(3,li))
dimnames(a) <- list(c("Mean","S.Dev","n"),1:li)
}
a <- formatC(a,digits=2,width=10,"f")
print(a, quote=F)
}
Thanks for your help.
R. Heberto Ghezzo Ph.D.
Meakins-Christie Labs
McGill University
Montreal - Canada
heberto at meakins.lan.mcgill.ca
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list