From SPSS to R, Rewriting Code for Survey Data Tabulation: MICS Survey Example
By: Filip Mitrovic
Tags: R, Surveys, Package, Coding, Spss

Next to defining the same variables: literate, literateP, tot, numMen, this syntax defines libraries needed to run spss *.sav file in R environment.

after that segment same computation as in SPSS syntax is performed: ### computing literacy as in SPSS syntax, re-written

```{r pressure, echo=TRUE} data = compute(data, {

literate = 2

}) data$literate<- ifelse((data$MWB6A >=2 & data$MWB6A <8), 1,

ifelse(data$MWB14==3,1,2)) # making data numeric in R dataframe for calculations

data$literate<-as.numeric(as.character(data$literate)) # adding value labels in R, comparable to 'value labels' function in SPSS.

val_lab(data$literate) = num_lab("

1 Literate

2 Illiterate

")

# adding variable with overall percent of literate respondents. This is comparable to 'compute' function in SPSS. All variables have identical names as in SPSS syntax for easier reference.

data = compute(data, {

literateP = 0

}) data$literateP<- ifelse(data$literate ==1,100,0) #recode(data$mwelevel) = c(0 ~ 0, 1 ~ 1, 2:3 ~ 2, 9 ~ 9, other ~ NA) # adds value labels to the column headings in the table in R. This is not in SPSS syntax as those are already pre-defined in the *sav dataset

data$mwelevel<-as.numeric(as.character(data$mwelevel))

val_lab(data$mwelevel) = num_lab("

1 Up to primary

2 Lower secondary

3 Upper Secondary or higher [A]

9 Don't know/ Missing

")

# computes additional columns as in SPSS syntax, for overall number of cases and a total percentage. 'numMen' and 'tot' are identical variable names as in SPSS syntax

data = compute(data, {

numMen = 1

}) data = compute(data, {

tot = 1

}) # adding variable labels. Labels added identical as in comparable SPSS syntax. In addition labels also added to the background characteristic variables that are shown in rows. This is not seen in SPSS syntax as they are already in the *.sav file.

data<-apply_labels(data,

literateP = "Total percentage literate [1]",

numMen = "Number of men",

HH6 = "Area", # up to variable 'mwelevel' all variables are background characteristics.

HH7 = "Region",

mdisability = "Functional difficulty",

religion = "Religion of the household head",

ethnicity = "Ethnicity of the household head",

windex5 = "Wealth quintile",

mwelevel = "Percent Distribution of highest level attended and literacy",

tot = "Total",

literate ="Literacy rate") # adding value labels as in SPSS. next to syntax specific value labels, that are shown in the comparative document, the labels for background characteristic variables are also added.

val_lab(data$tot) = num_lab("

1 | " ) val_lab(data$HH6) = num_lab("

1 Urban

2 Rural") val_lab(data$mdisability) = num_lab("

1 Has functinal difficulty

2 No funcitonal difficulty") val_lab(data$windex5) = num_lab("

1 Poorest

2 Second

3 Middle

4 Fourth

5 Richest") val_lab(data$religion) = num_lab("

1 Free Wesleyan Church

2 Latter Day Saints

3 Roman Catholic

4 Free Church of Tonga

5 Other religion

99 Don't know/missing") val_lab(data$ethnicity) = num_lab("

1 Tongan

2 Chinese

3 Fijian

4 Other ethnicity

99 Don'tknow/missing") val_lab(data$HH7) = num_lab("

1 Tongatapu

2 Vava'u

3 Ha'apai

4 'Eua

5 Ongo Niua")

But in addition to syntax specific variables, a set of variable names and labels is added too, as these are pre-defined in *sav file for SPSS but have to be introduced in the R code as well.

Now the tabulation syntax in SPSS is pretty straightforward and uses ‘ctables’ function to bind column level variables define din this syntax (‘literate’, ‘literateP’, ‘tot’, mwelevel’ and ‘numMen’) and tabulates it against pre-defined above mentioned background characteristic variables. Syntax looks like this:

ctables

/vlabels variables = layer mwelevel literate display = none

/table total[c]

+ hh6 [c]

+ hh7 [c]

+ $mage [c]

+ mdisability [c]

+ ethnicity [c]

+ religion [c]

+ windex5 [c]

by

layer [c] > mwelevel [c] > literate [c] [layerrowpct.validn '' f5.1] + tot [s] [mean '' f5.1] +

literateP [s] [mean '' f5.1]

+ numMen[s][sum '' f5.0]

/categories variables=all empty=exclude missing=exclude

/slabels position=column visible = no

/titles title=

"Table SR.6.1M: Literacy (men)"

"Percent distribution of men age 15-49 years by highest level of school attended and literacy, and the total percentage literate, " + surveyname

caption =

"[1] MICS indicator SR.2 - Literacy rate (age 15-24 years)"

"[A] Respondents who have attended upper secondary school or higher are considered literate and are not tested."

.

In R, tabulation would look like this:

```{r}

expss_output_viewer() # function to see the table in the viewer in R studio

data %>%

tab_total_row_position("none")%>% # suppresses the total values for each row. As SPSS syntax 'ctables' does not show totals, it is disabled in R code as well

tab_cells(tot, HH6, HH7, mdisability, ethnicity ,religion, windex5) %>% #defines rows for the table

tab_cols(literate, mwelevel, tot) %>% # defines columns to shoe percentage for with 'tab_stat_rpct'

tab_weight(weight = mnweight) %>% #adds weights from the dataset

tab_stat_rpct(total_label = NULL,total_statistic = "w_cases",)%>%

tab_cols(net(literateP, "Total percentage literate" = greater_or_equal(1), "TO_DELETE" = other))%>% #defines mean for the additional column as a different calculation method, and marks a columnt to be deleted

tab_stat_rpct(total_label = NULL,total_statistic = "w_cases",)%>%

tab_cols(total(numMen))%>% # last column and different calculation method only to count total cases

tab_stat_cases()%>%

tab_last_round(digits = get_expss_digits())%>%

tab_pivot(stat_position = "outside_columns")%>%

where(!grepl("TO_DELETE", row_labels)) %>%

drop_empty_rows()%>%

set_caption( "Table SR.6.1M: Literacy (men)

Percent distribution of men age 15-49 years by highest level of school attended and literacy, and the total percentage literate, " ) #adds caption on the top of the table

Final output of the R code looks like this in html format. Further customization can be done for word.