[R] abbreviate dataframe for Sweave output

Kuhn, Max Max.Kuhn at pfizer.com
Wed Feb 7 14:54:48 CET 2007


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}}



More information about the R-help mailing list