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 · 8 comments

Comments

Projects
None yet
7 participants
@christophergandrud
Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

christophergandrud commented Jul 14, 2015

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

@jabranham

This comment has been minimized.

Copy link

jabranham commented Mar 30, 2016

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

@hadley

This comment has been minimized.

Copy link
Member

hadley commented May 31, 2016

Fixed in #110

@hadley hadley closed this May 31, 2016

@ewenharrison

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

bac3917 commented Apr 4, 2018

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

@vjd

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment