pivot-tablesurvey

Please help me make cross tabs with survey data (basic)


I work with survey data which I get from a contractor (it's weighted). But I sometimes need different cross tabs from those they provide me. I don't currently have any budget for any software to do it, so I'd like to try using R.

I'm a real beginner with R, but this is a good excuse to spend time getting familiar.

Can anyone introduce me to a really basic guide to making crosstabs with weighted survey data from SAV files?

I tried to post what I've done so far but it thinks I'm typing code in the wrong format so I'll try my best.

I load it as an SAV file.

I need the labels otherwise I have this table (for gender)

  1   2   3 
571 561   1 

I checked the attributes which gets me to

label
[1] "S2. Gender."

format.spss:

[1] "F1.0"

Class

[1] "haven_labelled" "vctrs_vctr"     "double"        

Labels

         Male            Female             Other 
            1                 2                 3 

   Prefer not to say 
            4 

I've changed the original to factor and done CrossTable but that's a lot longer than what I want.

I tried table with gender (I converted to factor) and an output variable I chose at random and got this

           1   2   3   4   5
  Male    79 407  43  38   4
  Female  67 396  46  41  11
  Other    0   1   0   0   0`

That's closer to what I want, but I need the labels. Then I need to be able to include more elements, more along the left, more along the top.

Help please. Thank you


Solution

  • you use R and RStudio its free software.

    then install these packages

    install.packages("haven")

    install.packages("gmodels")

    install.packages("survey")

    install.packages("dplyr")

    then load them.

    library(haven) # Reads SPSS .sav files

    library(gmodels) # Creates crosstabs

    library(survey) # Handles weighted survey data

    library(dplyr) # Helps with data formatting

    then just load your survey data you might have to convert them into readable factor lables.(this is in R the CL)

    data <- data %>%

            mutate(across(where(is.labled), as_factor))
    

    -------------------------------------------------------------------

    to create a basic crosstab:

    CrossTable(data$Gender, data$YourVariable, prop.chisq = FALSE)

    if it includes weights:define a weighted design by replacing "weight_var" with the actual column name of the weight variable:

    design <- svydesign(ids = ~1, weights = ~weight_var, data = data)

    To create a weighted crosstab, use:

    svytable(~Gender + YourVariable, design)

    To get percentages instead of raw counts:

    prop.table(svytable(~Gender + YourVariable, design), margin = 1)

    This method allows you to work with weighted survey data without paying for expensive software like SPSS or Stata. Everything here is completely free, and once set up, it’s easy to generate custom cross-tabulations from your survey data. I've never done it but in theory this should work very basic.