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

Error: x and labels must be same type #86

Closed
christophergandrud opened this issue Jun 30, 2015 · 9 comments
Closed

Error: x and labels must be same type #86

christophergandrud opened this issue Jun 30, 2015 · 9 comments
Labels

Comments

@christophergandrud
Copy link

@christophergandrud christophergandrud commented Jun 30, 2015

Using read_dta on this Stata file produces odd output:

http://siteresources.worldbank.org/INTRES/Resources/469232-1107449512766/DPI2012.dta

One possible indication of the issues is running summary on the supposed data frame that read_dta creates produces:

Error: `x` and `labels` must be same type
@jon-mellon
Copy link

@jon-mellon jon-mellon commented Jul 8, 2015

I'm sure there will be a more permanent fix, but the following function I wrote cleared up this problem for me and might be useful as a stopgap:

labelDataset <- function(data) {
  correctLabel <- function(x) {

    if(!is.null(attributes(x)$labels)) {
      class(attributes(x)$labels) <- typeof(x)
    }
    return(x)
  }
  for(i in colnames(data)) {
    data[, i] <- correctLabel(data[, i])
  }
  return(data)
}
@christophergandrud
Copy link
Author

@christophergandrud christophergandrud commented Jul 14, 2015

Thanks. This largely addresses the issue. Though it would be nice if it was done internally.

@jabranham
Copy link

@jabranham jabranham commented Mar 30, 2016

I was having the same issue with stata data and this function solved it. Thanks!

@hadley
Copy link
Member

@hadley hadley commented May 31, 2016

Fixed in #110

@hadley hadley closed this May 31, 2016
@ewenharrison
Copy link

@ewenharrison ewenharrison commented Dec 20, 2017

I'm still having issues with this. I have a data.frame with variables of class labelled which I would rather keep. Still conflicting though. Any ideas appreciated. I'm not using haven but it loads with tidyverse hence issue.

# Simple linear regression
data(mtcars)
lm(mpg~cyl, data=mtcars) # Works as expected

# Add attribute
class(mtcars$mpg) = c("labelled",class(mtcars$mpg))
lm(mpg~cyl, data=mtcars) # Works as expected

library(haven)
lm(mpg~cyl, data=mtcars) #Error: `x` and `labels` must be same type
@bac3917
Copy link

@bac3917 bac3917 commented Apr 4, 2018

I'm running into this issue as well, running a basic subset command on a df.

@vjd
Copy link

@vjd vjd commented Sep 15, 2018

I still have this issue. However, I could resolve by loading tidyverse first and then other packages of interest. In this case I am using functions form Hmisc that are erroring out with this message if tidyverse is loaded after Hmisc.

@hadley
Copy link
Member

@hadley hadley commented Sep 16, 2018

This is fixed in the development version; you'll need to install that if you want the fix. You can track progress of the release at #399

@lock
Copy link

@lock lock bot commented Mar 15, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Mar 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
7 participants