Retrieving Variable Dictionary Information (R)
You can retrieve variable dictionary information from the active
dataset using functions specific to each type of information (such
as the variable label or the measurement level) or you can use the spssdictionary.GetDictionaryFromSPSS
function
to return results for a number of dictionary properties as an R data
frame. For
information on functions that retrieve specific dictionary properties,
see the topic on spssdictionary functions.
Example
DATA LIST FREE /id (F4) gender (A1) training (F1).
VARIABLE LABELS id 'Employee ID'
/training 'Training Level'.
VARIABLE LEVEL id (SCALE)
/gender (NOMINAL)
/training (ORDINAL).
VALUE LABELS training 1 'Beginning' 2 'Intermediate' 3 'Advanced'
/gender 'f' 'Female' 'm' 'Male'.
BEGIN DATA
18 m 1
37 f 2
10 f 3
END DATA.
BEGIN PROGRAM R.
vardict <- spssdictionary.GetDictionaryFromSPSS()
print(vardict)
END PROGRAM.
Result
X1 X2 X3
varName id gender training
varLabel Employee ID Training Level
varType 0 1 0
varFormat F4 A1 F1
varMeasurementLevel scale nominal ordinal
Each column of the returned data frame contains the information for a single variable from the active dataset. The information for each variable consists of the variable name, the variable label, the variable type (0 for numeric variables, and an integer equal to the defined length for string variables), the display format, and the measurement level.
Working wth the Data Frame Representation of a Dictionary
The data frame returned by the GetDictionaryFromSPSS
function contains the row labels varName, varLabel, varType, varFormat, and varMeasurementLevel. You
can use these labels to specify the corresponding row. For example,
the following code extracts the variable names:
varNames <- vardict["varName",]
It is often convenient to obtain separate lists of categorical and scale variables. The following code shows how to do this using the data frame representation of the IBM® SPSS® Statistics dictionary. The results are stored in the two R vectors scaleVars and catVars.
scaleVars<-vardict["varName",][vardict["varMeasurementLevel",]=="scale"]
catVars<-vardict["varName",][vardict["varMeasurementLevel",]=="nominal" |
vardict["varMeasurementLevel",]=="ordinal"]