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

Use repeated colnames #194

Closed
MarcoPortmann opened this issue Mar 12, 2024 · 6 comments
Closed

Use repeated colnames #194

MarcoPortmann opened this issue Mar 12, 2024 · 6 comments

Comments

@MarcoPortmann
Copy link

I think in combination with group headers it is common to have repeated colnames. What I want to achieve is "a" header a with subheader sub1 & sub2 and a header "b" with sub1 & sub2. Howver, I don't see a way to feed them into tinytable. dplyr::rename does not allow duplicates and, as far as I know, tt doesn't have an argument to set colnames.

 tibble(a_sub1 = 1, a_sub2 =2, b_sub1 = 3, b_sub2 = 4) %>% 
   tt() %>% 
   group_tt(j = list(a = 1:2, b = 3:4))
@vincentarelbundock
Copy link
Owner

Sorry, I don't understand what you mean. Could explain differently and ideally show me an example of a table like the one you want?

@MarcoPortmann
Copy link
Author

I'd like to achieve an output like this:

tibble(a_sub1 = 1, a_sub2 =2, b_sub1 = 3, b_sub2 = 4) %>% 
  rename(sub1 = "a_sub1", sub2 = "a_sub2", "sub1 " = "b_sub1", "sub2 " = "b_sub2")  %>% 
  tt() %>% 
  group_tt(j = list(a = 1:2, b = 3:4))

image

Howver, it would be nice to achieve it without the workaround of extra spaces in "sub1 " and "sub2 " which are required since a tibble cannot have two columns with identical colnames.

@vincentarelbundock
Copy link
Owner

tinytable already supports duplicated column names without any problem:

library(tinytable)
data.frame(sub1 = 1, sub2 = 2, sub1 = 3, sub2 = 4, check.names = FALSE) |>
    tt() |>
    group_tt(j = list(a = 1:2, b = 3:4))
a b
sub1 sub2 sub1 sub2
1 2 3 4

I’m closing this because this is not a limitation of tinytable, but rather a feature request for tibble() to allow duplicated column names.

@MarcoPortmann
Copy link
Author

MarcoPortmann commented Mar 12, 2024

Well, I'd argue, it makes sense that a tibble doesn't allow duplicated column names because a tibble is employed for calculus, data storage etc. and it should be machine-readible. tt aims at prodcuing human-friendly output and offers group_tt to contain a part of the column name. In this context it makes sense, in my opinion to have dpulicated headers. Btw., I encountered the issues as I was translating code from kable to tt. kable offers the argument col.names to set the column names.
But I guess, I could also live with the workaround that I used above.

@vincentarelbundock
Copy link
Owner

Actually, it looks like tibble does in fact support repeated names, and has a specific argument for it:

tibble(sub1 = 1, sub2 = 2, sub1 = 3, sub2 = 4, .name_repair = "minimal") |>
    tt() |>
    group_tt(j = list(a = 1:2, b = 3:4))

@MarcoPortmann
Copy link
Author

MarcoPortmann commented Mar 12, 2024

You're right. Interesting. It seems that .name_repair = "minimal" can be provided to tibblebut not to other functions like rename. Renaming via colnames(tab) <- works as well.

I guess the issue can be closed.

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