Skip to content

gather converts non-gathered factors to integers #104

@backlin

Description

@backlin

When gathering a number of columns in a data frame, the format of ungathered columns should remain unaffected, but it appears that ungathered factor columns are converted to integers with tidyr 0.3.0 (I use vanilla R 3.2.2 under Ubuntu 14.04).

Create example data

> df <- data.frame(Name = gl(2, 2, 8, c("Alice", "Bob")),
                   A = sample(8), B = sample(8), C = sample(8))
> str(df)
'data.frame':   8 obs. of  4 variables:
 $ Name: Factor w/ 2 levels "Alice","Bob": 1 1 2 2 1 1 2 2
 $ A   : int  3 5 4 7 2 6 1 8
 $ B   : int  7 5 6 2 4 3 1 8
 $ C   : int  8 6 3 4 7 5 1 2

Gather and inspect

> df %>% gather(Col, Value, A:C) %>% str
'data.frame':   24 obs. of  3 variables:
 $ Name : int  1 1 2 2 1 1 2 2 1 1 ...
 $ Col  : Factor w/ 3 levels "A","B","C": 1 1 1 1 1 1 1 1 2 2 ...
 $ Value: int  3 5 4 7 2 6 1 8 7 5 ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions