Peter Wielhouwer
Peter Wielhouwer

Reputation: 1

Repeated haven_labelled error. What am I not doing correctly?

I am relatively inexperience in r and am trying to do work that's relatively simple, but get repeated similar errors. For example, I am working with a dataset and trying to generate a simple crosstabulation of two variables using crosstable in an attached dataset. The dataset was imported from an SPSS .sav file.

crosstable(battle04, region)

Error in crosstable(battle04, region) : 1 assertions failed:

crosstable(abortlaw, region)

Error in crosstable(abortlaw, region) : 1 assertions failed:

This is not the first time I've had an error message along these lines and am perplexed what the problem is.

Upvotes: 0

Views: 141

Answers (1)

Dan Chaltiel
Dan Chaltiel

Reputation: 8523

crosstable() needs as the first argument the dataset, and as the second argument a vector of columns to describe.

For instance, if I want to describe the columns Sepal.Length and Species from the dataset iris2, you should write this:

library(crosstable)
crosstable(iris2, c(Sepal.Length, Species))
#> # A tibble: 7 x 4
#>   .id          label           variable   value        
#>   <chr>        <chr>           <chr>      <chr>        
#> 1 Sepal.Length Length of Sepal Min / Max  4.3 / 7.9    
#> 2 Sepal.Length Length of Sepal Med [IQR]  5.8 [5.1;6.4]
#> 3 Sepal.Length Length of Sepal Mean (std) 5.8 (0.8)    
#> 4 Sepal.Length Length of Sepal N (NA)     150 (0)      
#> 5 Species      Specie          setosa     50 (33.33%)  
#> 6 Species      Specie          versicolor 50 (33.33%)  
#> 7 Species      Specie          virginica  50 (33.33%)

crosstable(iris2, c(Sepal.Length, Species)) %>% as_flextable()

enter image description here

Created on 2022-11-10 with reprex v2.0.2

In your case, the error message says that in your code crosstable(battle04, region), battle04 should be a dataset (of class data.frame) but it is a vector of class haven_labelled/vctrs_vctr/double.

As @stefan said in the comments, we will need a sample of your dataset to help you with some code.

Upvotes: 0

Related Questions