Suppose the following data.frame
:
sexo<-(1,1,0,1,0) # 1: hombre; 0:mujer
# asignacion de los valores para ruido, olores, contaminacion (características de la vivienda y su entorno):
# 1: mucho; 2:algo; 3:nada
ruido<-c(1,3,3,1,2) # ruido molesto del exterior
olores<-c(2,2,3,3,1) # malos olores en la vivienda
contaminacion<-c(2,3,1,2,3) # contaminacion en el entorno de la vivienda
acceso_agua<- c(2,3,1,2,2) # la vivienda NO dispone de acceso a agua de calidad
fuma<-c(1,0,1,0,0) # 1:si; 0:no
datos_modelo<-cbind.data.frame(ruido,olores,contaminacion,acceso_agua)
It is desired to recode the variables ruido
, olores
, contaminacion
and acceso_agua
in such a way that the values 2 and 3 take the value 0, that is, to convert these variables to binary.
I am currently doing it as follows:
attach(datos_modelo)
datos_modelo$ruido[ruido == 2 | ruido == 3] <- 0
datos_modelo$olores[olores == 2 | olores == 3] <- 0
datos_modelo$acceso_agua[acceso_agua == 2 | acceso_agua == 3] <- 0
However, this code is a reproducible example of my problem, as I actually have 14 variables that I want to recode in the same way into a data.frame
40+ variable . The variables I want to recode are all together inside the data.frame, so it's possible to call them all together by their indices, in this example:datos_modelo[2:4]
How can I do the recoding more efficiently?: With fewer lines of code and not as repetitively.
A very simple way, as long as the values are numbers of type 1/0, is to use a bit of matrix arithmetic.
Basically we are going to leave the values that are already 1 at 1, and everything else at 0, so we generate a logic vector, where
FALSE
are those values other than 1, then when multiplying the original values with this matrix, those willFALSE
end up being the 0 we are looking for.