Re: merge table rows (\multirow)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: merge table rows (\multirow)

Fabian Scheipl-2
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 in the "which"-columns of data.frame "df" 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)
        }
)

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: merge table rows (\multirow)

Fabian Scheipl-2
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)
        }
)
______________________________________________
[hidden email] 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.