Skip to content

Column silently dropped when its name conflicts with value col name in gather.data.frame() #347

Closed
@jarodmeng

Description

@jarodmeng

gather.data.frame() computes which columns to gather using this snippet.

  quos <- quos(...)
  if (is_empty(quos)) {
    gather_vars <- setdiff(names(data), c(key_var, value_var))
  } else {
    gather_vars <- unname(tidyselect::vars_select(names(data), !!! quos))
  }

When the user doesn't provide a ... argument, the gather columns are computed to be anything that's not key or value. This logic works when there's no existing column having the same name as value. If there is, that column would be silently dropped from the gathering.

I've produced a simple reprex below to illustrate.

packageVersion("tidyr")
#> [1] '0.7.0'

library(tidyr)
library(tibble)

XYZ <- data.frame(
  X = rnorm(2, 0, 1),
  Y = rnorm(2, 0, 2),
  Z = rnorm(2, 0, 4)
)

gather(XYZ, name, value)
#>   name      value
#> 1    X  0.1051179
#> 2    X  1.4617993
#> 3    Y -0.9121167
#> 4    Y  2.5378557
#> 5    Z -2.1496032
#> 6    Z -2.4815070

gather(XYZ, name, Y)
#>            Y name          Y
#> 1 -0.9121167    X  0.1051179
#> 2  2.5378557    X  1.4617993
#> 3 -0.9121167    Z -2.1496032
#> 4  2.5378557    Z -2.4815070

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorpivoting ♻️pivot rectangular data to different "shapes"

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions