Recode

Copy, Paste and Adapts

dat$V8 = recode(dat$V8, " 'F'='M'; 'M'='H'; else=NA" )

dat$V6 = recode(dat$V6, " lo:10='low'; 11:hi='hig' ")

dat$V5 = recode(dat$V5, " NA=-999 ")

dat$V5 = recode(dat$V5, " -999=NA ")

Objetive

Recode in the same or different variable, from user-specified criteria.

  1. recode of one variable through the car library

  2. recode of múltiples variables at the same time with multiple.recode.fnc (P.Prieto & Juan A. Hernández)

recode

OBrienKaiser$gender= recode(OBrienKaiser$gender,

" 'M'='male'; 'F'='female'; else=NA " )

We create the new variable gender on OBrienKaiser data from the recoding of the gender variable originally with 2 levels (M and F). In the new variable M will be male, F female and any other value we will missing value (NA)

Suppose you have a variable called electrode with 29 levels (channels) in the database erp and we want to create another variable: hemisphere with two levels: left and right.

A quick and efficient way to do this recoding is:

  1. We create two vectors (left and right) with the names of the electrodes we want to recode.

  2. Whith recode we indicated that elements of the right and left vector will have the 'left' and 'right' label respectively in the new recoded variable-hemisphere.

left= c("F7","F3","FC5","FC1","T7",'C3','CP5','CP1',"P7","P3")

right= c("F8","F4","FC2",'FC6',"T8","C4",'CP2','CP6','P8','P4')

erp$hemisphere=recode(erp$channel," left= 'left';

right= 'right'; else=NA ", levels=c('left','right'))

recode rules

  • recode function is open and close always with character " (double quotes)

  • Each of the recodings must be separated by the character semicolon (;)

  • Use the else (optional) subcommand to assign the rest of the possible values not included in the above recodings to missing value (else = NA).

  • Use subcomand levels if you want an specific order for the factor levels

2.- multiple.recode.fnc

RECODIFICION MÚLTIPLE SOBRE LAS MISMAS VARIABLES

Recodificar p variables todas con los mismos valores de recodificación. Por ejemplo en muchas ocasiones sobre todo con cuestionarios, es muy común la existencia de items que se han invertido para evitar la deseabilidad social del sujeto que responde. En estas circunstancias, lo normal es reinvertir los valores de la variable para que todos los items pertenecientes a una misma escala presenten el mismo "sentido". La forma mas rápida y eficiente de lograrlo es con la función creada a tal efecto multiple.recode.fnc (P.Prieto y Juan A. Hernández) que lleva a cabo precisamente esta labor.

Utilizaremos como ejemplo la base de datos bfi (2800 sujetos) de la librería psych perteneciente al cuestionario autoadministrado Synthetic Aperture Personality Assessment (SAPA) con 25 items en 5 escalas: Conformidad, Autoconsciencia, Extraversión, Neuroticismo y Apertura. En dichas escalas, los items 1,9,11,12,22 y 25 están invertidos. Veamos el procedimiento de reinvertirlos al mismo sentido que el resto de los items.

bfi= multiple.recode.fnc(bfi,

variables=c(1,9,11,12,22,25),

old.value=1:6, new.value=6:1)

#------------------------------------------------------------------

# RECODIFICACION MULTIPLE DE VARIABLES

#------------------------------------------------------------------

*** Se han recodificado las siguientes variables:

[1] "A1" "C4" "E1" "E2" "O2" "O5"

*** desde los valores originales: 1 2 3 4 5 6

*** a los nuevos: 6 5 4 3 2 1

Observa que hemos incluido dos argumentos nuevos: old.value y new.value relativos a los valores originales y a los nuevos recodificados.

RECODIFICION MÚLTIPLE SOBRE VARIABLES DIFERENTES.

Si desear conservar las variagles originales y por lo tanto recodificar en variable diferente debes incluir el argumento new.variable=T.

bfi= multiple.recode.fnc(bfi,

variables=c(1,9,11,12,22,25),

old.value=1:6, new.value=6:1,

new.variable=T)

#------------------------------------------------------------------

# RECODIFICACION MULTIPLE DE VARIABLES

#------------------------------------------------------------------

*** Se han recodificado las siguientes variables:

[1] "A1" "C4" "E1" "E2" "O2" "O5"

*** En estas nuevas variables:

[1] "A1.r" "C4.r" "E1.r" "E2.r" "O2.r" "O5.r"

*** desde los valores originales: 1 2 3 4 5 6

*** a los nuevos: 6 5 4 3 2 1

Si las nuevas variables a crear ya existiesen en la base de datos (por una recodificación anterior) la función te dará el siguiente mensaje de error, indicándote que debes o renombrar la o las variables de la base de datos o incluir el argumento replace=T para sustituir las antiguas variables por las nuevas.

#------------------------------------------------------------------

# RECODIFICACION MULTIPLE DE VARIABLES

#------------------------------------------------------------------

*** Error. Parece que esas variables han sido anteriormente recodificadas,

*** puesto que ya existen en la base de datos. Estas son esas variables:

[1] "A1.r" "C4.r" "E1.r" "E2.r" "O2.r" "O5.r"

*** Renombralas si deseas conservarlas con change.var.name.fnc

*** o incluye el argumento replace=T en la llamada a la función.

bfi= multiple.recode.fnc(bfi,

variables=c(1,9,11,12,22,25),

old.value=1:6, new.value=6:1,

new.variable=T, replace=T)