0

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:

  • Variable 'data': Must be of type 'data.frame', not
  • 'haven_labelled/vctrs_vctr/double'.

crosstable(abortlaw, region)

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

  • Variable 'data': Must be of type 'data.frame', not 'double'.

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

  • What package is `crosstable` from? – Ben Bolker Nov 09 '22 at 16:39
  • I *think* dplyr. https://cran.r-project.org/web/packages/crosstable/vignettes/crosstable.html – Peter Wielhouwer Nov 09 '22 at 17:51
  • 2
    Welcome to SO, Peter. In general it would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data. However, after a look at the docs and from the errors you get I would guess `battle04` or `abortlaw` are names of variables in your dataset. However, ´crossable` expects a dataframe as its first argument, while the columns come as the second argument, i.e. try something like `crosstable(NAME_OF_DATASET, c(battle04, region))`. – stefan Nov 09 '22 at 18:36
  • In fact I think it's the `crosstable` package – Ben Bolker Nov 09 '22 at 18:42
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Nov 09 '22 at 19:26

1 Answers1

0

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.

Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92