0

I would like to make a table in R that I can convert into Latex Code with the months as the names of the rows, and the Category as the name of the columns. In each "cell" that corresponds to a month and category, I would like to have meantotrob2 and stdtotrob2 below meantotrob2 in small parentheses. Here is what part of my dataframe looks like.

label Category meantotrob2 stdtotrob2
  <fct>    <dbl>       <dbl>      <dbl>
1 April        1      0.122       0.361
2 April        2      0.121       0.288
3 April        3      0.123       0.297
4 April        4      0.0996      0.248
5 May          1      0.0878      0.206
6 May          2      0.0776      0.182
Months     1       2          3
# April 0.12162162 0.12111801  0.12278761
        **0.361**   **0.288**  **0.297**
# May   0.08783784 0.07763975  0.09734513
         **0.206** **0.182**   **0.259**

This is what I'm looking for. Standard errors are in bold. Also, here is my head of the dataframe

structure(list(label = structure(c(1L, 1L, 1L, 1L, 2L, 2L), .Label = c("April", 
"May", "June", "July(1-17)", "July(18-31)", "August", "September", 
"October", "November", "December"), class = "factor"), Category = c(1, 
2, 3, 4, 1, 2), meantotrob2 = c(0.121621621621622, 0.12111801242236, 
0.122787610619469, 0.0995575221238938, 0.0878378378378378, 0.077639751552795
), stdtotrob2 = c(0.361428596863379, 0.287914904504829, 0.297434540363719, 
0.248141256461657, 0.205954891705483, 0.181650473189414)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L), groups = structure(list(
    label = structure(1:2, .Label = c("April", "May", "June", 
    "July(1-17)", "July(18-31)", "August", "September", "October", 
    "November", "December"), class = "factor"), .rows = structure(list(
        1:4, 5:6), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L), .drop = TRUE))
  • Please make your question reproducible: include a sample of your data with the output of `dput(your_data)` or `dput(head(your_data, n = 10))` – Peter Mar 08 '22 at 05:28

1 Answers1

1

Edited: You are looking for an answer that needs multiple steps, you need to understand what your LaTeX table would look like first to create a data frame that looks like it to print it out as a LaTeX table.

Here's a path:

  1. Melt your data.
melt(df,id = c("Label","Category"),variable.name = "Type",value.name = "value")
  1. Then unmelt your data for categories: check this post
  2. Sort your data with order
  3. Rename Label as Month using colnames.
  4. Clear the month names from the rows that have the standard deviations.
  5. Deal with the standard deviation strings with paste when Type=="stdtorob2"
paste0("**",as.character(foo),"**")
  1. Remove the Type column because you don't need it. With tidyverse df %>% select(-Type) should do it.
  2. And finally to print the table as LaTeX code using xtable. Try this:
library(xtable)
print(xtable(df,digits=4), include.rownames=FALSE)
evolozzy
  • 13
  • 6
  • You know how in linear regression tables they have the standard deviation below the mean? Essentially I am trying to do that. – Neel Gupta Mar 08 '22 at 05:46
  • See the edited post. You need to create a new string and print that. – evolozzy Mar 08 '22 at 06:08
  • Hey sorry, I updated the post with what I'm looking for since it wasn't very clear I think. I'm not trying to replicate my dataframe as a table. – Neel Gupta Mar 08 '22 at 07:31
  • value.var="value" gives an error since "value" is now a character column. not sure what I can do? as.numeric deletes the SDs – Neel Gupta Mar 08 '22 at 23:31
  • I see. Skip the first step and deal with it before deleting the Type column – evolozzy Mar 09 '22 at 02:05
  • Added new edits. Again, this is a multiple step question, when asking a question try to break it down to steps and look for answers that way. It will be easier for you to find an answer that way. – evolozzy Mar 09 '22 at 02:13