[R] Xtable with long column headings/names

BR_email br at dmstat1.com
Tue May 16 12:56:52 CEST 2017


John:
Did not mean to take a short-cut, but I thought the code was not needed.
Your follow-up is appreciated. Here's the code. I want the column headings
  to wrap around into two lines, not one long heading.
Any help is greatly sought. Thanks. Bruce
~~

Response <- rbinom(50,1,0.2)
yhat     <- runif(50)
data     <- data.frame(Response,yhat)
data     <- data[order(data$yhat,decreasing=TRUE),]
cum_R    <- cumsum(data$Response)
sam_size <- nrow(data)
cum_n    <- seq(1:1,sam_size)
wt       <- rep(c(1), times=sam_size)
cum_wt   <- cumsum(wt)
dec      <- (cum_n/sam_size)
decc     <- floor((cum_n*10)/(sam_size+1))
dec_mean <- aggregate(data$Response, by=list(decc), mean)
dd_      <- data.frame(cum_R, sam_size, cum_wt, cum_n, decc)
dd       <- data.frame(Response, dd_)
dec_mean <- aggregate(data$Response ~ decc, dd, mean)
wt       <- rep(c(1), times=sam_size)
cum_wt   <- aggregate(wt        ~ decc, dd, sum)
cum_R    <- aggregate(data$Response  ~ decc, dd, sum)
dec_mean_wt    <- cbind(dec_mean, cum_wt)
dec_mean_wt    <- dec_mean_wt[-3]
dec_mean_wt_R  <- cbind(dec_mean_wt, cum_R)
dec_mean_wt_R  <- dec_mean_wt_R[-4]
colnames(dec_mean_wt_R) <- c("Decile", "Avg_Response", "No_Individuals",
                              "Total_Response")
dec_mean_wt_R <- dec_mean_wt_R[,c(1,2,3,4)]
cum_n         <- dec_mean_wt_R[3]
cum_n         <- cumsum(cum_n)
library(plyr)
Cum_Inds      <- rename(cum_n, c(No_Individuals="Cum_Inds"))
Cum_Response  <- dec_mean_wt_R[4]
Cum_Response  <- cumsum(Cum_Response)
library(plyr)
Cum_Total_Response  <- rename(Cum_Response, 
c(Total_Response="Cum_Total_Response"))
dec_mean_wt_R_nR    <- cbind(dec_mean_wt_R, Cum_Inds, Cum_Total_Response)
Cum_Decile_Response <- Cum_Total_Response/ Cum_Inds
library(plyr)
Cum_Decile_Response <-rename(Cum_Decile_Response, 
c(Cum_Total_Response="Cum_Decile_Response"))
dec_mean_wt_R_nRD   <- cbind(dec_mean_wt_R_nR, Cum_Decile_Response)
avg_RR              <- dec_mean_wt_R_nRD[10,7]
Cum_Lift            <- (Cum_Decile_Response/avg_RR)*100
library(plyr)
Cum_Lift <-rename(Cum_Lift, c(Cum_Decile_Response="Cum_Lift"))
DECILE              <- c("top","2","3","4","5","6","7","8","9","bot")
dec_mean_wt_R_nRDL  <- cbind(dec_mean_wt_R_nRD, Cum_Lift,DECILE)
options(digits=3)

dec_mean_wt_R_nRDL  <- dec_mean_wt_R_nRDL[,c(9,3,4,2,7,8)]
total_line<-cbind(DECILE="Total",
   as.data.frame(matrix(c(colSums(dec_mean_wt_R_nRDL[ , 2:3]), rep(NA, 
3)),nrow=1)))

names(total_line)   <-names(dec_mean_wt_R_nRDL)
dec_mean_wt_R_nRDLT <-rbind(dec_mean_wt_R_nRDL,total_line)
decile_table        <- dec_mean_wt_R_nRDLT
library(plyr)
dec_analy           <- rename(decile_table,
        c(No_Individuals="NUMBER OF INDIVIDUALS",
          Total_Response="NUMBER OF RESPONDERS",
          Avg_Response="RESPONSE RATE (%)",
          Decile_RespRate="RESPONSE RATE (%)",
          Cum_Decile_Response="CUM RESPONSE RATE (%)" ,
          Cum_Lift="CUM LIFT"))
#Install the xtable package: install.packages("xtable")
#Load the xtable package:
library(xtable)
DECILE_TABLE <-xtable(dec_analy, digits = c(0,0,0,0,2,2,0),
                       align = "ccccccc", latex.environments = "center",
                       caption = "Decile Analysis based on Y (RESPONSE) 
regressed on X1 X2 X3")
DECILE_TABLE

print.xtable(DECILE_TABLE, type="html",file="C:/R_Data/DecileTable.html",
              include.rownames = FALSE,
              caption.placement = "top",
              align = "p{.5in}p{.5in}p{.5in}p{.5in}p{.5in}p{.5in}")


John Kane wrote:
> Hi Bruce,
> I accidentally replied just to you and we should be replying to the 
> list especially since there are a lot of people there who know a lot 
> more about xtabs than I do. My bad.
>
> I use xtabs from time to time and love it but I occasionally feel that 
> I am groping in the dark when I try anything fancy.
>
> What we need is an actual working example so we would need not only 
> the table code but a sample of the data with some of the long titles 
> included.
>
> I'd suggest resubmitting the table information with some sampled data 
> to the list.  Have a look at Reproducibility · Advanced R. 
> <http://adv-r.had.co.nz/Reproducibility.html> for some suggestions on 
> how to put one together.
>
> In particular, some sample data in dput form ( see ?dput) would be 
> best. Data in dput() format is essentially an exact copy of your data 
> and avoids confusion in reading in data. that is a factor remains a 
> factor, character data remains character and so on.
>
>
>
>
> 	
>
>
>     Reproducibility · Advanced R.
>
> 	
>
> <http://adv-r.had.co.nz/Reproducibility.html>
>
>
>
>
>
>
> On Monday, May 15, 2017 1:40 PM, BR_email <br at dmstat1.com> wrote:
>
>
> John:
> Here is the code of my xtable:
> TABLE <-xtable(dec_analy, digits = c(0,0,0,0,2,2,0),
>                       align = "ccccccc", latex.environments = "center",
>                       caption = "Analysis ")
> print.xtable(TABLE, type="html",file="C:/R_Data/Table.html",
>               include.rownames = FALSE,
>               caption.placement = "top",
>               align = "p{.5in}p{.5in}p{.5in}p{.5in}p{.5in}p{.5in}")
>
>
>
> BR_email wrote:
> > John:
> > After I generate the xtable table, I manually edited (by inserting
> > <br/>) the html header, below.
> > Is there a way in xtable that I can set some setting to affect my
> > xtable table?
> > Thanks for your reply.
> > Bruce
> >
> > <table border=1>
> > <caption align="top"> Analysis </caption>
> > <tr> <th> DECILE </th>
> > <th> NUMBER OF <br /> INDIVIDUALS <br /> </th>
> > <th> NUMBER OF <br /> RESPONDERS <br /> </th>
> > <th> RESPONSE <br /> RATE (%) <br /> </th>
> > <th> CUM RESPONSE <br /> RATE (%) <br />
> > </th> <th> CUM LIFT </th> </tr>
> >
> >
> >
> > John Kane wrote:
> >> Can you give us an example. I am having a problem visualizing this.
> >> It seems obvious just to put in a line break normally but in xtabs
> >> who knows?
> >>
> >>
> >> On Monday, May 15, 2017 1:15 PM, Bruce Ratner PhD <br at dmstat1.com 
> <mailto:br at dmstat1.com>>
> >> wrote:
> >>
> >>
> >> R-help:
> >> I'm using xtable that produces a table in html with one-line for each
> >> of the
> >> long column headings/names.
> >>
> >> I would like to word wrap the column headings to break into two-lines.
> >> Any suggestion as to which argument needs adjustment is appreciated.
> >> Bruce
> >>
> >> ______________________________________________
> >> R-help at r-project.org <mailto:R-help at r-project.org> 
> <mailto:R-help at r-project.org <mailto:R-help at r-project.org>> mailing 
> list -- To
> >> UNSUBSCRIBE and more, see
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide
> >> http://www.R-project.org/posting-guide.html 
> <http://www.r-project.org/posting-guide.html>
> >> <http://www.r-project.org/posting-guide.html>
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >>
> >
>
>
>



More information about the R-help mailing list