Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Column names corresponding to the columns from row_labels when writing to excel? #71

Open
vinhdizzo opened this issue Oct 8, 2020 · 2 comments

Comments

@vinhdizzo
Copy link

Hi,

I've been summarizing data for various reports and forcing my tables as etable objects in order to leverage xl_write from the expss package to write out my tables.

I have a question: is there a way to specify the column names for the columns generated from the row_labels variable of an etable object?

Here's an example:

> foo <- mtcars %>% 
    tab_cells(cyl) %>% 
    tab_cols(vs) %>% 
    tab_stat_cpct() %>% 
    tab_pivot() %>% 
    tab_caption("Simple column percent")
foo <- mtcars %>% 
+     tab_cells(cyl) %>% 
+     tab_cols(vs) %>% 
+     tab_stat_cpct() %>% 
+     tab_pivot() %>% 
+     tab_caption("Simple column percent")
> str(foo)
Classes 'with_caption', 'etable' and 'data.frame':	4 obs. of  3 variables:
 $ row_labels: chr  "cyl|4" "cyl|6" "cyl|8" "cyl|#Total cases"
 $ vs|0      : num  5.56 16.67 77.78 18
 $ vs|1      : num  71.4 28.6 NA 14
 - attr(*, "caption")= chr "Simple column percent"
> foo
 Simple column percent                                     
 |     |              |   vs |      |
 |     |              |    0 |    1 |
 | --- | ------------ | ---- | ---- |
 | cyl |            4 |  5.6 | 71.4 |
 |     |            6 | 16.7 | 28.6 |
 |     |            8 | 77.8 |      |
 |     | #Total cases | 18.0 | 14.0 |

Is there a way to set the first two column names to be c1 and c2, similar to:

 Simple column percent                                     
 |     |              |   vs |      |
 | c1  | c2           |    0 |    1 |
 | --- | ------------ | ---- | ---- |
 | cyl |            4 |  5.6 | 71.4 |
 |     |            6 | 16.7 | 28.6 |
 |     |            8 | 77.8 |      |
 |     | #Total cases | 18.0 | 14.0 |

I'm going to assume no, and if so, could I request this be incorporated? Based on how an etable object is structured, perhaps we could incorporate this using a call like set_column_names_of_row_labels('c1|c2') that one could pipe the etable object into, where the function adds a column_names_of_row_labels attribute, similar to how set_caption sets the caption attribute.

Thank you for your considerations.

@gdemin
Copy link
Owner

gdemin commented Oct 8, 2020

You could use setNames to set name for first column:

library(expss)
foo <- mtcars %>% 
    tab_cells(cyl) %>% 
    tab_cols(vs) %>% 
    tab_stat_cpct() %>% 
    tab_pivot() %>% 
    tab_caption("Simple column percent") %>% 
    setNames(., c("c1|c2", colnames(.)[-1]))

But there is an issue - labels for the first column don't split with "|". And that's why they all stay in the first column.
I will look how I can fix it without breaking something.

@vinhdizzo
Copy link
Author

Ahh, I thought the row_labels column had to be called row_labels. Being able to rename that column would definitely make it consistent with the other columns.

Let me know when you've fully incorporate this and I will test it out. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants