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

gather converts non-gathered factors to integers #104

Closed
backlin opened this issue Sep 8, 2015 · 7 comments
Closed

gather converts non-gathered factors to integers #104

backlin opened this issue Sep 8, 2015 · 7 comments

Comments

@backlin
Copy link

backlin commented Sep 8, 2015

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 ...
@backlin
Copy link
Author

backlin commented Sep 8, 2015

I assume this is a bug since gather of tidyr 0.2.0 behaves as one would expect, i.e. it does not convert the column Name to integer.

@iugrina
Copy link

iugrina commented Sep 8, 2015

I see the same thing (tidyr 0.3.0 + R 3.2.1/3.2.2).

@vnijs
Copy link

vnijs commented Sep 8, 2015

Same thing happens on Mac (R 3.2.2). Seems I missed this when testing the pre-release version of tidyr

## with tidyr 0.3
tab <- structure(list(variable = structure(1:5, .Label = c("price", "carat", "depth", "table", "x"), class = "factor"), mean = c(3907.186, 
  0.794283333333333, 61.7526666666667, 57.4653333333333, 5.72182333333333
  )), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
  -5L), .Names = c("variable", "mean"))

tab %>% gather("function", "value", -1)
tab %>% gather("function", "value", -variable)

## with tidyr 0.2
tab %>% gather("function", "value", -1)
Source: local data frame [5 x 3]

  variable function        value
    (fctr)   (fctr)        (dbl)
1    price     mean 3907.1860000
2    carat     mean    0.7942833
3    depth     mean   61.7526667
4    table     mean   57.4653333
5        x     mean    5.7218233

@ghost
Copy link

ghost commented Sep 9, 2015

same with the dev version 0.3.0.9000 :(

devtools::install_version("tidyr", version = "0.2.0", repos = "http://cran.us.r-project.org")

works again

@vnijs
Copy link

vnijs commented Sep 9, 2015

@ckluss Try the last commit for version 0.2.0

devtools::install_github("hadley/tidyr", ref = "2e08772d154babcc97912bcae8b0b64b65b964ab")

@hadley hadley closed this as completed in 8492570 Sep 9, 2015
@vnijs
Copy link

vnijs commented Sep 9, 2015

That works. Thanks @hadley

@ghost
Copy link

ghost commented Sep 9, 2015

cool, thank you very much for the fast fix @hadley

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

3 participants