xpssRecode: Recodes variables

Description Usage Arguments Details Value Author(s) Examples

View source: R/xpssRecode.R

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

1
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.

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.
else=99 overwrites all unmatched values with 99, only possible for numeric values.

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

Value

A xpssFrame with the recoded variables.

Author(s)

Andreas Wygrabek

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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"))

translateSPSS2R documentation built on May 30, 2017, 4:31 a.m.