head() function failing

501 views Asked by At

I want to use the head() function to have an overview of the data set but it doesn't work.

> library(haven)
> corona <- read_sav("Corona_FB/Corona_FB/TRR265_CoronaFB_16-12-2020.sav")
> View(corona)
> head(corona)

Error in is.character(subclass) : argument "subclass" is missing, with no default

> head(corona,n=3)

Error in is.character(subclass) : argument "subclass" is missing, with no default

1

There are 1 answers

0
Ben Bolker On

Since your object is a tibble (class "tbl_df" "tbl" "data.frame"), which is a very widely used object class, the problem is probably with the head() function/method. In a clean R session (i.e., no other objects in the workspace), with only the haven and tidyverse packages loaded, this works fine:

library(tidyverse)
library(haven)
dd <- tibble(x=1:5,y=2:6)
head(dd)

There is about a 99% chance that you have somehow gotten a weird version of head defined (a 1% chance that your data object (corona) is weird). To be sure that it's not your data object, we would need a reproducible example, i.e. you would have to give us access to your data file or to your data object dumped with save() or dput()).

If find("head") returns anything other than "package:utils", then you do indeed have a different head() function masking the base-R version. You can make sure you are using the base-R version by using utils::head() instead of head(). If the weird version of head() is in your global workspace (i.e. find("head") returns ".GlobalEnv") then you may want to get rid of it by saying rm("head").

Alternatively, as suggested in the comments, you could see if your code works in a clean R session, i.e. one where you have no other packages loaded or objects defined in your workspace. (Restarting R usually works, but you need to make sure that you're not restoring a workspace from a previous R session.)

In the meantime, if you want an "overview" of your data set, you could also try summary() or str().