Skip to contents

This class is amending haven::labelled_spss with a unique object identifier id to make later binding or joining reproducible and well-documented.

Usage

labelled_spss_survey(
  x = double(),
  labels = NULL,
  na_values = NULL,
  na_range = NULL,
  label = NULL,
  id = NULL,
  name_orig = NULL
)

as_character(x)

is.labelled_spss_survey(x)

as_numeric(x)

Arguments

x

A vector to label. Must be either numeric (integer or double) or character.

labels

A named vector or NULL. The vector should be the same type as x. Unlike factors, labels don't need to be exhaustive: only a fraction of the values might be labelled.

na_values

A vector of values that should also be considered as missing.

na_range

A numeric vector of length two giving the (inclusive) extents of the range. Use -Inf and Inf if you want the range to be open ended.

label

A short, human-readable description of the vector.

id

Survey ID

name_orig

The original name of the variable. If left NULL it uses the latest name of the object x.

Details

It inherits many methods from labelled, but uses more strict coercion and validation rules.

See also

as_factor

Other type conversion functions: as_labelled_spss_survey()

Other type conversion functions: as_labelled_spss_survey()

Examples

x1 <- labelled_spss_survey(
  1:10, c(Good = 1, Bad = 8), 
  na_values = c(9, 10), 
  id = "survey1")
  
is.na(x1)
#>  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE

# Print data and metadata 
print(x1)
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> attr(,"labels")
#> Good  Bad 
#>    1    8 
#> attr(,"na_values")
#> [1]  9 10
#> attr(,"class")
#> [1] "retroharmonize_labelled_spss_survey" "haven_labelled_spss"                
#> [3] "haven_labelled"                     
#> attr(,"survey1_name")
#> [1] "1:10"
#> attr(,"survey1_values")
#>  1  2  3  4  5  6  7  8  9 10 
#>  1  2  3  4  5  6  7  8  9 10 
#> attr(,"survey1_labels")
#> Good  Bad 
#>    1    8 
#> attr(,"survey1_na_values")
#> [1]  9 10
#> attr(,"id")
#> [1] "survey1"

x2 <- labelled_spss_survey( 1:10, 
 labels  = c(Good = 1, Bad = 8), 
 na_range = c(9, Inf),
 label = "Quality rating", 
 id = "survey1")


is.na(x2)
#>  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE

# Print data and metadata
x2
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> attr(,"labels")
#> Good  Bad 
#>    1    8 
#> attr(,"label")
#> [1] "Quality rating"
#> attr(,"na_range")
#> [1]   9 Inf
#> attr(,"class")
#> [1] "retroharmonize_labelled_spss_survey" "haven_labelled_spss"                
#> [3] "haven_labelled"                     
#> attr(,"survey1_name")
#> [1] "1:10"
#> attr(,"survey1_values")
#>  1  2  3  4  5  6  7  8  9 10 
#>  1  2  3  4  5  6  7  8  9 10 
#> attr(,"survey1_label")
#> [1] "Quality rating"
#> attr(,"survey1_labels")
#> Good  Bad 
#>    1    8 
#> attr(,"survey1_na_range")
#> [1]   9 Inf
#> attr(,"id")
#> [1] "survey1"