How to make sub-headers in R

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

How to make sub-headers in R

Liao, Hongsheng
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]]

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

Re: How to make sub-headers in R

Jeff Newmiller
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:<[hidden email]>        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" <[hidden email]> 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]]
>
>______________________________________________
>[hidden email] 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.

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

Re: How to make sub-headers in R

Bert Gunter
In reply to this post by Liao, Hongsheng
Hank:

1. No.

2. You would do well to go through an R tutorial -- the "Introduction
to R" ships with R, but there are many more on the Web --  as you
appear to be applying spreadsheet type concepts to R's data.frame data
structure. While there certainly is a resemblance, conflating the two
is a grave error that will get you into a lot of trouble. If you wish
to use R, learn R; don't intuit or presume.

3. One way to simulate what you want to do is to attach a "subhead"
attribute to your data frame, class this structure( e.g.
c("frame_with_subhead","data.frame") ) and write a (S3)
print.frame_with_subhead method to print objects of the class in some
suitable way. See ?attributes, ?attr, and ?UseMethod. However, all
these man pages will be terse and assume that you have done the
aforementioned homework of learning R. You could, of course, use S4
classes or any other OO system available for R instead of S3.

HTH.

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll




On Fri, May 15, 2015 at 6:55 AM, Liao, Hongsheng <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.

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

Re: How to make sub-headers in R

Boris Steipe
In reply to this post by Liao, Hongsheng

On May 15, 2015, at 9:55 AM, Liao, Hongsheng <[hidden email]> 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

The elements of a data frame's columns are all of the same type. Inserting an extra row with character values would coerce the entire column to be of type character:

myDf <- data.frame(a=c(1:3),b=letters[1:3], C=LETTERS[1:3], stringsAsFactors=FALSE)
sapply(myDf, class)
          a           b           C
  "integer" "character" "character"
myDf <- rbind(c("text1", "text2", "text3"), myDf)
myDf
      a     b     C
1 text1 text2 text3
2     1     a     A
3     2     b     B
4     3     c     C

sapply(myDf, class)
          a           b           C
"character" "character" "character"

In addition, you shouldn't be thinking about the "header" as being a textual description anyway: the colname is actually much like a variable name, a label to identify a column. If you think of an Excel file (I think this is where your question is coming from), the names are the A, B ... Z, AA, AB ... names. It just so happens that R can construct column names from data it finds in a "header row". But they are _attributes_ of the data frame, not part of the contents. However you can look at the attributes ...
attributes(myDf)
$names
[1] "a" "b" "C"

$row.names
[1] 1 2 3 4

$class
[1] "data.frame"

attributes(myDf)$names
[1] "a" "b" "C"


... and you can add additional attributes:

attributes(myDf)$subheaders <- c("text1", "text2", "text3")

attributes(myDf)
$names
[1] "a" "b" "C"

$row.names
[1] 1 2 3 4

$class
[1] "data.frame"

$subheaders
[1] "text1" "text2" "text3"


Hope this helps.
Boris







>
> Hongsheng (Hank) Liao, PhD.
> Lab Manager
> Center for Quantitative  Fisheries Ecology
> Old Dominion University
> 757-683-4571
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

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

Re: How to make sub-headers in R

Liao, Hongsheng
In reply to this post by Jeff Newmiller
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:[hidden email]]
Sent: Friday, May 15, 2015 10:43 AM
To: Liao, Hongsheng; [hidden email]
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:<[hidden email]>        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" <[hidden email]> 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]]
>
>______________________________________________
>[hidden email] 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 03OseHrzs) is spam:
Spam:        https://www.spamtrap.odu.edu/canit/b.php?i=03OseHrzs&m=a356b01b838f&t=20150515&c=s
Not spam:    https://www.spamtrap.odu.edu/canit/b.php?i=03OseHrzs&m=a356b01b838f&t=20150515&c=n
Forget vote: https://www.spamtrap.odu.edu/canit/b.php?i=03OseHrzs&m=a356b01b838f&t=20150515&c=f
------------------------------------------------------
END-ANTISPAM-VOTING-LINKS

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

Re: How to make sub-headers in R

Boris Steipe
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 <[hidden email]> 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:[hidden email]]
> Sent: Friday, May 15, 2015 10:43 AM
> To: Liao, Hongsheng; [hidden email]
> 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:<[hidden email]>        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" <[hidden email]> 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]]
>>
>> ______________________________________________
>> [hidden email] 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 03OseHrzs) is spam:
> Spam:        https://www.spamtrap.odu.edu/canit/b.php?i=03OseHrzs&m=a356b01b838f&t=20150515&c=s
> Not spam:    https://www.spamtrap.odu.edu/canit/b.php?i=03OseHrzs&m=a356b01b838f&t=20150515&c=n
> Forget vote: https://www.spamtrap.odu.edu/canit/b.php?i=03OseHrzs&m=a356b01b838f&t=20150515&c=f
> ------------------------------------------------------
> END-ANTISPAM-VOTING-LINKS
>
> ______________________________________________
> [hidden email] 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.

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

Re: How to make sub-headers in R

Liao, Hongsheng
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:[hidden email]]
Sent: Friday, May 15, 2015 11:42 AM
To: Liao, Hongsheng
Cc: [hidden email]
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 <[hidden email]> 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:[hidden email]]
> Sent: Friday, May 15, 2015 10:43 AM
> To: Liao, Hongsheng; [hidden email]
> 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:<[hidden email]>        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" <[hidden email]> 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]]
>>
>> ______________________________________________
>> [hidden email] 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.
>
>
>
> --
>
> ______________________________________________
> [hidden email] 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

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

Re: How to make sub-headers in R

Boris Steipe
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 <[hidden email]> 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:[hidden email]]
> Sent: Friday, May 15, 2015 11:42 AM
> To: Liao, Hongsheng
> Cc: [hidden email]
> 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 <[hidden email]> 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:[hidden email]]
>> Sent: Friday, May 15, 2015 10:43 AM
>> To: Liao, Hongsheng; [hidden email]
>> 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:<[hidden email]>        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" <[hidden email]> 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]]
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>
>>
>>
>> --
>>
>> ______________________________________________
>> [hidden email] 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

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

Re: How to make sub-headers in R

Liao, Hongsheng
Hi Boris,
First of all, thank you for persisting to help me on the issue.  Second, I didn't know paste() could take a function, and thanks for teaching me about it even I didn't use it this time. Finally, I have found the solution based on your code.    Following are what I have done:

1. Using your age vector  ages <- c(1, 1, 2, 3, 5, 8) as data, I typed them in following code to generate the output I want

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 & 1 & 1 & 2 & 3 & 5 & 8", paste("\\", "\\", sep=""), paste("\n"),
      "\\hline \n",
      "\\endhead \n",
                             "\\hline \n",
                             "{\\footnotesize To continue} \n",
                             "\\endfoot \n",
                                        "\\endlastfoot \n",sep=""))


2. Then, I wrote a loop as follows:  
ages <- c(1, 1, 2, 3, 5, 8)
subtitle <- "Interval"
for(i in 1:length(ages)){
subtitle <- paste(subtitle, "&", ages[i])
}

3. Put the object "subtitle" in the following code:

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",
      subtitle, paste("\\", "\\", sep=""), paste("\n"),
      "\\hline \n",
      "\\endhead \n",
                             "\\hline \n",
                             "{\\footnotesize To continue} \n",
                             "\\endfoot \n",
                                        "\\endlastfoot \n",sep=""))

4. Both codes generate the exact same result as follows:
> age.addtorow
$pos
$pos[[1]]
[1] 0


$command
[1] "\\hline \n\\endfirsthead \n\\multicolumn{4}{l}{{Table \\thetable\\ Continued}} \\\\\n\\hline \n\\multicolumn{7}{r}{{Age}}\\\\\n\\hline \nInterval & 1 & 1 & 2 & 3 & 5 & 8\\\\\n\\hline \n\\endhead \n\\hline \n{\\footnotesize To continue} \n\\endfoot \n\\endlastfoot \n"

5. Since the vector "ages" can be updated from the database of different species and years, my problem is solved.

Thank you very much again.  Now I can go home happily and have a good weekend.  You have a wonderful weekend, too.

Hank

Hongsheng (Hank) Liao, PhD.
Lab Manager
Center for Quantitative  Fisheries Ecology
Old Dominion University
757-683-4571






-----Original Message-----
From: Boris Steipe [mailto:[hidden email]]
Sent: Friday, May 15, 2015 2:35 PM
To: Liao, Hongsheng
Cc: [hidden email]
Subject: Re: [R] How to make sub-headers in R

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 <[hidden email]> 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:[hidden email]]
> Sent: Friday, May 15, 2015 11:42 AM
> To: Liao, Hongsheng
> Cc: [hidden email]
> 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-headi
> ngs-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 <[hidden email]> 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:[hidden email]]
>> Sent: Friday, May 15, 2015 10:43 AM
>> To: Liao, Hongsheng; [hidden email]
>> 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:<[hidden email]>        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" <[hidden email]> 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]]
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>
>>
>>
>> --
>>
>> ______________________________________________
>> [hidden email] 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 03OsiBGtq) is spam:Spam:        https://www.spamtrap.odu.edu/canit/b.php?i=03OsiBGtq&m=c21d21b85f81&t=20150515&c=sNot spam:    https://www.spamtrap.odu.edu/canit/b.php?i=03OsiBGtq&m=c21d21b85f81&t=20150515&c=nForget vote: https://www.spamtrap.odu.edu/canit/b.php?i=03OsiBGtq&m=c21d21b85f81&t=20150515&c=f
------------------------------------------------------
END-ANTISPAM-VOTING-LINKS

______________________________________________
[hidden email] 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.