[R] How to make sub-headers in R
Boris Steipe
boris.steipe at utoronto.ca
Fri May 15 20:34:37 CEST 2015
Hongsheng -
If you can define the fish ages programmatically, you can also get the values into your output. I had understood your "sub-header" to be constant per column. Does it need to be updated for different rows? Or are you just missing the fact that paste() can take variables, and even functions like so:
R > myAge <- function(x) { x*x }
R > ages <- c(1, 1, 2, 3, 5, 8)
R > paste("Interval ",
" & 1", # in string
" & ", 2, # constant
" & ", ages[4], # from vector
" & ", ages[5],
" & ", myAge(5), # from function
"\n", sep="")
[1] "Interval & 1 & 2 & 3 & 5 & 25\n"
Cheers,
Boris
On May 15, 2015, at 2:15 PM, Liao, Hongsheng <HLiao at odu.edu> wrote:
> Thank you very much for trying to help me. Using following R codes, I can generate a LaTeX long table which can repeat "Age" and "Interval & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & Totals" as title and subtitle across multiple pages. However, the part of "0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11" is for fish ages (I am working on fish) which vary between species and years within each species, and R can't update this part according to different species and years. As a result, I have to manually update it in either my R or LaTeX codes, which I am trying to avoid in order to save time and to minimize errors. It looks like there is no solution to my problem so far. Anyway, thank you again and have a good weekend.
>
> age.addtorow <- list()
> age.addtorow$pos <- list()
> age.addtorow$pos[[1]] <- c(0)
> age.addtorow$command <- c(paste("\\hline \n",
> "\\endfirsthead \n",
> "\\multicolumn{4}{l}{{", paste("Table", "\\thetable\\ Continued}}", paste("\\", "\\", sep="")), paste("\n"),
> "\\hline \n",
> "\\multicolumn{7}{r}{{Age}}", paste("\\", "\\", sep=""), paste("\n"),
> "\\hline \n",
> "Interval & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & Totals", paste("\\", "\\", sep=""), paste("\n"),
> "\\hline \n",
> "\\endhead \n",
> "\\hline \n",
> "{\\footnotesize To continue} \n",
> "\\endfoot \n",
> "\\endlastfoot \n",sep=""))
>
> print(x.age.composition, file=output.age.composition.location.file, include.rownames = FALSE,
> include.colnames = TRUE, hline.after = c(-1, -1, 0, 1, nrow(x.age.composition), nrow(x.age.composition)), type="latex",
> append=FALSE, floating=FALSE, tabular.environment = "longtable", na.print = "", caption.placement = "top",
> sanitize.colnames.function = force, add.to.row = age.addtorow))
>
>
> -----Original Message-----
> From: Boris Steipe [mailto:boris.steipe at utoronto.ca]
> Sent: Friday, May 15, 2015 11:42 AM
> To: Liao, Hongsheng
> Cc: r-help at r-project.org
> Subject: Re: [R] How to make sub-headers in R
>
> I don't see that being an option in xtable ... but looking at this:
> http://tex.stackexchange.com/questions/33510/how-do-i-create-the-headings-for-this-multirow-multicolum-table
> ... it seems to be pretty straightforward to write a function that writes LaTeX output from your dataframe for the Tex multirow package.
>
>
> B.
>
> On May 15, 2015, at 11:18 AM, Liao, Hongsheng <HLiao at odu.edu> wrote:
>
>> Thanks for your response. I want to make a LaTeX table with a title and subtitles. Attached is an example made using Word. I can use "xtable()" and "print()" to generate a LaTeX table of it with "Age" as title. However, I would like to make the second row "Interval (Inch) ........." as sub-title so that both the title and sub-title can be repeated on every page in my long table split among multiple pages. In LaTeX, "\endhead" can repeat title but not the row of "Interval ..." because it is not title or sub-title. I can copy and paste "Interval..." above "\endhead" to get the repetition of "Interval...". However, I am trying to learn how to avoid "copy and paste", instead, let LaTeX do the job automatically. I know that I am making the question more complicated than my original one and hope it is clear enough this time.
>>
>> Hongsheng (Hank) Liao, PhD.
>> Lab Manager
>> Center for Quantitative Fisheries Ecology Old Dominion University
>> 757-683-4571
>>
>>
>>
>>
>>
>>
>>
>> -----Original Message-----
>> From: Jeff Newmiller [mailto:jdnewmil at dcn.davis.CA.us]
>> Sent: Friday, May 15, 2015 10:43 AM
>> To: Liao, Hongsheng; r-help at r-project.org
>> Subject: Re: [R] How to make sub-headers in R
>>
>> I think you are not interpreting what is happening correctly. Column names are labels used for purposes of referring to the data in your R code. That they might also be useful in presenting data in output is coincidental. The fact that many data input functions replace spaces in those labels with periods should convince you of this fact.
>>
>> On the other hand, the options available when you output that table usually depend on where you want to display the result, which you have not mentioned. For example the tables package has many options for labeling columns if you are generating HTML or LaTeX output. Or, you could write your own function to generate any output format you want.
>> ---------------------------------------------------------------------------
>> Jeff Newmiller The ..... ..... Go Live...
>> DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
>> Live: OO#.. Dead: OO#.. Playing
>> Research Engineer (Solar/Batteries O.O#. #.O#. with
>> /Software/Embedded Controllers) .OO#. .OO#. rocks...1k
>> ----------------------------------------------------------------------
>> ----- Sent from my phone. Please excuse my brevity.
>>
>> On May 15, 2015 6:55:17 AM PDT, "Liao, Hongsheng" <HLiao at odu.edu> wrote:
>>> I know how to make one-row header for a data frame using "colnames".
>>> Is there any function to insert sub-header between the first row of
>>> the data and the header? Thanks
>>>
>>> Hongsheng (Hank) Liao, PhD.
>>> Lab Manager
>>> Center for Quantitative Fisheries Ecology Old Dominion University
>>> 757-683-4571
>>>
>>>
>>>
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> 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
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>>
>> --
>>
>> ______________________________________________
>> 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
>> and provide commented, minimal, self-contained, reproducible code.
>
>
>
> --
> BEGIN-ANTISPAM-VOTING-LINKS
> ------------------------------------------------------
> Teach CanIt if this mail (ID 01OsfJgXE) is spam:Spam: https://www.spamtrap.odu.edu/canit/b.php?i=01OsfJgXE&m=d0e228a83e81&t=20150515&c=sNot spam: https://www.spamtrap.odu.edu/canit/b.php?i=01OsfJgXE&m=d0e228a83e81&t=20150515&c=nForget vote: https://www.spamtrap.odu.edu/canit/b.php?i=01OsfJgXE&m=d0e228a83e81&t=20150515&c=f
> ------------------------------------------------------
> END-ANTISPAM-VOTING-LINKS
More information about the R-help
mailing list