[R] merge table rows (\multirow)

Fabian Scheipl Fabian.Scheipl at stat.uni-muenchen.de
Mon Jul 26 17:07:04 CEST 2010


You can also automate it with this:

do.multirow<-function(df, which=1:ncol(df)){
    for(c in which){
        runs <- rle(as.character(df[,c]))
        if(all(runs$lengths>1)){
            tmp <- rep("", nrow(df))
            tmp[c(1, 1+head(cumsum(runs$lengths),-
1))] <-
                    paste("\\multirow{",runs$lengths,"}{*}{",df[c(1,
1+head(cumsum(runs$lengths),-1)),c],"}",sep="")
            df[,c] <- tmp
        }
    }
    return(df)
}

This will replace the "which"-columns of data.frame "df" that have
only repeated entries  with the appropriate \multirow commands.

You then have to use print.xtable with sth like
print(xtable(do.multirow(df)),
        sanitize.text.function = function(x){
            return(x)
        }
)


More information about the R-help mailing list