Learn R Programming

translateSPSS2R (version 1.0.0)

xpssRecode: Recodes variables

Description

R implementation of the SPSS RECODE Function. xpssRecode recodes atomics or vectors of the format numeric, character or factor under the terms of recode specifications.

Usage

xpssRecode(x, variables, rec = NULL, varout = NULL)

Arguments

x
a (non-empty) data.frame or input data of class "xpssFrame".
variables
atomic character or character vector with the names of the variabless to recode.
rec
character string with recoding specifications: for more informations see details.
varout
atomic character or character vector with the names of new variables.

Value

A xpssFrame with the recoded variables.

Details

The input of the recoding is a character string with the recoding procedure seperated with a semicolon and a optional else statement.
single data transformation:
rec = "1 = 99; else = test"

For a numeric vector transformation:
rec = "c(1,2,3) = 1; else = 11"

For a character vector transformation:
rec = "c('A','B') = 'AB'; c('C','D') = 'CD'; else = 'ZZ'"

For a range of values:
rec = "lo:10 = 1; 11:22 = 2; 23:hi = 3; else = 'copy'".

NOTE: lo and hi are special values and determine the lowest and highest value of a numeric variable. The ":"-Operator differs in this context from the sequence operator. In xpssRecode it specifies the range from A to B. F.e. 1:10 defines the range from 1 till 10, all values which are within this range get recoded.

The else statement indicates the handling of the values which are not selected by the recoding statement, this statement matches all unspecified values, including missing values. System default, if no else statement is given, is else='copy'.

else='copy' overwrites all unmatched values with the original value. else='NA' overwrites all unmatched values in the new dataset with NA. else='Other' overwrites all unmatched values with Other, only possible for character values.

varout determines whether a new variable with the recoded values should appended at the end of the dataset.

Examples

Run this code
data(fromXPSS)
fromXPSS <- xpssRecode(fromXPSS,
           variables ="V1",
           rec="'Audi' = 'Porsche'; else= copy",
           varout = NULL)

fromXPSS <- xpssRecode(fromXPSS,
                   variables = c("V5","V7_2"),
                   rec = "lo:50 =1; 51:200=2; 201:hi=3; else = copy",
                   varout =c("V5_new","V7_new"))

fromXPSS <- xpssRecode(fromXPSS,
                   variables = c("V6_kl3","V7_2"),
                   rec = "sysmis = 99",
                   varout =c("V6_new","V7_new"))

Run the code above in your browser using DataLab