[R] abbreviate dataframe for Sweave output

stubben stubben at lanl.gov
Thu Feb 8 18:16:49 CET 2007


Thanks Charles and Max, both functions work great.  I also used a hack 
to replace row names with '.', '. ', '.  ', etc.

Chris

dot.df <- function(x, head = 3, tail=1, dotrows=2)
{
    x <- format(rbind(head(x,head + dotrows), tail(x,tail)))
    if(dotrows>0)
    {
       x[(head + 1):(head + dotrows),] <- "."
       for(i in 1:dotrows){ rownames(x)[head+i]<-paste(".", substring("  
         ", 1, i-1))}
    }
    x
}

dot.df(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



## or dot.matrix (replacing latex commands for display here)

dot.matrix( crabs)
     sp sex ...   CW   BD
1    B   M ...   19    7
2    B   M ... 20.8  7.4
.    .   .  .     .    .
199  O   F ... 48.7 19.8
200  O   F ... 52.5 21.1


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



More information about the R-help mailing list