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
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
Chris,
You might be able to get it using format. You would have to convert the
data frame to a matrix if you want the row names to be dots too.
foo <- function(x, top = 3, ...)
{
if(dim(x)[1] < top + 3) stop("not enough rows")
charX <- format(x, ...)
charX <- charX[c(1:(top+2), dim(charX)[1]), ]
charX[(top + 1):(top + 2),] <- "."
charX <- as.matrix(charX)
rownames(charX)[(top + 1):(top + 2)] <- "."
charX
}
> library(MASS)
> foo(crabs)
sp sex index FL RW CL CW BD
1 "B" "M" " 1" " 8.1" "
6.7" "16.1" "19.0" " 7.0"
2 "B" "M" " 2" " 8.8" "
7.7" "18.1" "20.8" " 7.4"
3 "B" "M" " 3" " 9.2" "
7.8" "19.0" "22.4" " 7.7"
. "." "." "." "." "."
"." "." "."
. "." "." "." "." "."
"." "." "."
200 "O" "F" "50" "23.1"
"20.2" "46.2" "52.5" "21.1"
Max
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of stubben
Sent: Tuesday, February 06, 2007 6:39 PM
To: r-help at stat.math.ethz.ch
Subject: [R] abbreviate dataframe for Sweave output
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.
----------------------------------------------------------------------
LEGAL NOTICE\ Unless expressly stated otherwise, this messag...{{dropped}}