[R] abbreviate dataframe for Sweave output
Charles C. Berry
cberry at tajo.ucsd.edu
Wed Feb 7 02:11:55 CET 2007
Chris,
Not precisely what you want (does both rows and columns), but can be
specialized to your case easily.
Use with xtable in a chunk with 'results=tex' as in
xtable( dot.matrix( diag(10) ) )
Chuck
dot.matrix <- function(x , rws=NULL, cls=NULL, in.math=FALSE )
{
## Purpose: prepare a matrix like object for xtable printing with
## embedded cdots and vdots to stand in for omitted rows and columns
##
----------------------------------------------------------------------
## Arguments: x - a data.frame or matrix,
## rws - rows to include , cls - cols to include ,
## in.math - in math mode? dont use $
##
----------------------------------------------------------------------
## Author: CCB, Date: 13 Jan 2007, 12:54
if (length(rws)==1) {def.rows <- rws;rws <- NULL} else def.rows <- 2
if (length(cls)==1) {def.cols <- cls;cls <- NULL} else def.cols <- 2
if (in.math) {
cdot <- "\\cdots"
vdot <- "\\vdots"
ddot <- "\\ddots"
} else {
cdot <- "$\\cdots$"
vdot <- "$\\vdots$"
ddot <- "$\\ddots$"
}
x <- as.data.frame(x)
if (is.null(rws)) {
rws <- rownames(x)
if (is.null(rws)) rws <- seq(nrow(x))
rws <- list(head=head(rws,def.rows),tail=tail(rws,def.rows))
}
if (is.null(cls)) {
cls <- colnames(x)
if (is.null(cls)) cls <- seq(ncol(x))
cls <- list(head=head(cls,def.cols),tail=tail(cls,def.cols))
}
safe.dots <- structure(list(factor(cdot,c(cdot,ddot))),names=cdot,row.names=as.integer(1),class="data.frame")
head.rows <- cbind( x[ rws$head, cls$head ,drop=FALSE ], safe.dots,
x[ rws$head, cls$tail ,drop=FALSE ])
mid.row <- structure(matrix(rep(c(vdot,ddot,vdot),c(length(cls$head),1,length(cls$tail))),nr=1),
dimnames=list(vdot,colnames(head.rows)))
tail.rows <- cbind( x[ rws$tail, cls$head ,drop=FALSE ], safe.dots, x[ rws$tail, cls$tail ,drop=FALSE ] )
tab <- rbind(head.rows,mid.row,tail.rows)
tab
}
On Tue, 6 Feb 2007, stubben wrote:
> I wanted to print the first and last rows of some dataframes in Sweave
> using dots in columns to separate the two parts. Head and tail almost
> work, but I have problems with factors and row names.
>
> z<-data.frame(id=letters[1:26], x=sample(1:26,26))
>
> rbind(head(z,3), ".", tail(z,1))
>
> id x
> 1 a 18
> 2 b 8
> 3 c 14
> 4 <NA> .
> 26 z 10
> Warning message:
> invalid factor level, NAs generated in...
>
>
> I would like something like this if possible. Any ideas?
>
> id x
> 1 a 18
> 2 b 8
> 3 c 14
> . . .
> . . .
> 26 z 10
>
>
> Thanks,
>
> Chris Stubben
>
>
>
> --
> -----------------
>
> Los Alamos National Lab
> BioScience Division
> MS M888
> Los Alamos, NM 87545
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://biostat.ucsd.edu/~cberry/ La Jolla, San Diego 92093-0901
More information about the R-help
mailing list