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

Tibble allows duplicate column names after rename(). #70

Closed
ghost opened this issue Aug 13, 2018 · 8 comments · Fixed by #116
Closed

Tibble allows duplicate column names after rename(). #70

ghost opened this issue Aug 13, 2018 · 8 comments · Fixed by #116
Labels
bug an unexpected problem or unintended behavior rename

Comments

@ghost
Copy link

ghost commented Aug 13, 2018

move[bot] commented on Jul 21, 2018, 9:58 PM UTC:

@kovla commented on Jul 12, 2018, 9:35 AM UTC:

I imagine that rename() of a column to a name that already exists in the tibble (in another column) should warrant at least a warning. Otherwise it can result in two indistinguishable variables, with same name and type:

x <- tibble(a = 1:3, b = 4:6, c = 7:9)
x %>% rename(b = c)

# A tibble: 3 x 3
      a     b     b
  <int> <int> <int>
1     1     4     7
2     2     5     8
3     3     6     9

This issue was moved by krlmlr from tidyverse/tibble#431.

This issue was moved by romainfrancois from tidyverse/dplyr#3723.

@ghost
Copy link
Author

ghost commented Aug 13, 2018

@krlmlr commented on Jul 21, 2018, 9:59 PM UTC:

Thanks, confirmed. We shouldn't create duplicates here.

@ghost
Copy link
Author

ghost commented Aug 13, 2018

@romainfrancois commented on Aug 13, 2018, 8:50 AM UTC:

Is this a tidyselect issue @lionel- ?

> dplyr:::rename.data.frame
function(.data, ...) {
  vars <- tidyselect::vars_rename(names(.data), !!!quos(...))
  select_impl(.data, vars)
}
<bytecode: 0x10f1e6ed0>
<environment: namespace:dplyr>
> x <- tibble(a = 1:3, b = 4:6, c = 7:9)
> tidyselect::vars_rename(names(x), b = c)
  a   b   b 
"a" "b" "c" 

We can be defensive in dplyr otherwise, but I feel vars_rename should be.

@ghost
Copy link
Author

ghost commented Aug 13, 2018

@lionel- commented on Aug 13, 2018, 9:03 AM UTC:

It looks like it.

@krlmlr
Copy link
Member

krlmlr commented Aug 13, 2018

What do you suggest that tibble should do about it?

@romainfrancois
Copy link
Collaborator

I'm suggesting that tidyselect::vars_rename result should not have the same name more than once.

@krlmlr
Copy link
Member

krlmlr commented Aug 13, 2018

Thought this is a tibble issue now... 🤦‍♂️ 🤦‍♂️

@profandyfield
Copy link

There are times you might want duplicate names. For example, creating a tibble to pass to kable() or apa_table() in papaja. Let's say your table will show mean and CI of some outcome measures for treatment and control groups. Different measures are in rows of the table, named in column 1. Column 2 is mean for treatment, column 3 the CI for treatment, column 4 the mean for control, column 5 the CI for control. You're going to put column spans above the header row to denote treatment (cols 2 & 3) and control (cols 4 and 5). You'd want column 2 and 4 in the tibble to both be labelled 'Mean' and columns 3 and 5 to both be labelled '95% CI' because (and I could be wrong) in apa_table you can't override the variable names from the tibble you pass into it. Even if I'm wrong about that, it makes senseto have an option to override a default of no duplicate names IMO.

@lionel- lionel- added the rename label Sep 9, 2019
@lionel-
Copy link
Member

lionel- commented Sep 11, 2019

@profandyfield You'd probably want to use nested tibbles in that case (i.e. a whole data frame in a column):

tibble(
  measure = c("foo", "bar"),
  treatment = tibble(mean = 1:2, se = rnorm(2)),
  control = tibble(mean = 3:4, se = rnorm(2))
)
#> # A tibble: 2 x 3
#>   measure treatment$mean    $se control$mean    $se
#>   <chr>            <int>  <dbl>        <int>  <dbl>
#> 1 foo                  1 -0.453            3 -0.577
#> 2 bar                  2  0.239            4 -1.18

@lionel- lionel- added the bug an unexpected problem or unintended behavior label Sep 11, 2019
lionel- added a commit to lionel-/tidyselect that referenced this issue Sep 11, 2019
lionel- added a commit to lionel-/tidyselect that referenced this issue Oct 4, 2019
lionel- added a commit to lionel-/tidyselect that referenced this issue Oct 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior rename
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants