[R] Xtable with long column headings/names
BR_email
br at dmstat1.com
Tue May 16 17:38:38 CEST 2017
John:
Sorry, but I do not understand what you have done.
It seems like you hard coded the format I want, correct?
Bruce
John Kane wrote:
> Hi Bruce,
> We don't need all that code :) All that is required is the data.fame
> being used in the xtable command and the actual xtable commands. Below
> is what I think is a complete "minimal working example" for your
> problem. Note that in the print.xtable command I have removed the file
> path as it will be meaningless to other users. It gave me an error as
> I don't even have a C: drive--I use linux.
>
> I'd suggest posting it as a follow-up to your original question on
> R-help and going from there. I have done a bit of Googling and it
> looks, perhaps, possible to do what you want but it's beyond my
> knowledge level at the moment. I "might" be able to figure it out but
> in days whereas the real gurus on the R-help list may do it in 30 seconds.
>
> Shifting to another topic, you are using "data" as the name of a
> data.frame which is not a good idea as there is a function in R called
> "data". It is possible to over-write functions in R with"
> interesting" results. If you are feeling really adventurous try FALSE
> <- TRUE and see what happens.
>
> You also have 4 calls to plyr in the code and there is what I think is
> a duplication of code below one of the library(plyr) calls, probably a
> duplicate paste.. I'd suggest putting all library() or require()
> calls at the top of the code.
>
> And to be incredibly picky, you have a colnames () command at roughly
> line 27:30 for a data.frame and names() is the usual (preferred?) way
> to do this. colnames() is more used for a matrix. I "think" names() is
> more more generic though you would have to ask someone who knows what
> they are talking about it--not me-- to be sure.
>
> Sorry I cannot be of more help.
>
>
> ##===========================================================
> library(xtable)
> dec_analy <- structure(list(DECILE = structure(c(10L, 1L, 2L, 3L, 4L, 5L,
> 6L, 7L, 8L, 9L, 11L), .Label = c("2", "3", "4", "5", "6", "7",
> "8", "9", "bot", "top", "Total"), class = "factor"), `NUMBER OF
> INDIVIDUALS` = c(5,
> 5, 5, 5, 5, 5, 5, 5, 5, 5, 50), `NUMBER OF RESPONDERS` = c(2,
> 1, 2, 0, 0, 0, 0, 1, 0, 1, 7), `RESPONSE RATE (%)` = c(0.4, 0.2,
> 0.4, 0, 0, 0, 0, 0.2, 0, 0.2, NA), `CUM RESPONSE RATE (%)` = c(0.4,
> 0.3, 0.333333333333333, 0.25, 0.2, 0.166666666666667, 0.142857142857143,
> 0.15, 0.133333333333333, 0.14, NA), Cum_Total_Response = c(NaN,
> 714.285714285714, 714.285714285714, 714.285714285714, 714.285714285714,
> 714.285714285714, 714.285714285714, 714.285714285714, 714.285714285714,
> 714.285714285714, NA)), .Names = c("DECILE", "NUMBER OF INDIVIDUALS",
> "NUMBER OF RESPONDERS", "RESPONSE RATE (%)", "CUM RESPONSE RATE (%)",
> "Cum_Total_Response"), row.names = c(NA, 11L), class = "data.frame")
>
> 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",
> include.rownames = FALSE,
> caption.placement = "top",
> align = "p{.5in}p{.5in}p{.5in}p{.5in}p{.5in}p{.5in}")
> #============================================================
>
>
> On Tuesday, May 16, 2017 6:56 AM, BR_email <br at dmstat1.com> wrote:
>
>
> 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
> <mailto: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>
> > <mailto: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>>
> > <mailto: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>
> > >> <http://www.r-project.org/posting-guide.html>
> > >> and provide commented, minimal, self-contained, reproducible code.
> > >>
> > >>
> > >
> >
> >
> >
>
>
>
More information about the R-help
mailing list