I'm trying to recode the variables in my datasets. The data is messy and consisted of mixed classes. And I want to tidy all of them into binary numeric variables with 1/0. I have produced a simplified example as follows:
My original data consist of variables of classes character (yes/no), logical (TRUE/FALSE) and numeric (1/0). I want to code everything into 1/0, and the missing values as 0 as well.
tmp <- data.frame(x1 = c("Yes","Yes","No","No",NA),
x2 = c(TRUE, TRUE, FALSE, FALSE, NA),
x3 = c(1,1,0,0,NA))
tmp$x1 <- as.character(tmp$x1)
recode.var <- function(x){
if (is.character(x)) {
x <- ifelse(x=="Yes",1,ifelse(x=="No",0,ifelse(is.na(x),0,NA)))
}
if (is.logical(x)) {
x <- ifelse(x==TRUE,1,ifelse(x==FALSE,0,ifelse(is.na(x),0,NA)))
}
if (is.numeric(x)) {
x <- ifelse(x==1,1,ifelse(x==0,0,ifelse(is.na(x),0,NA)))
}
x <- as.numeric(x)
return(x)
}
tmp1 <- data.frame(apply(tmp, 2, recode.var))
However, the result is not what I wished.
> tmp1
x1 x2 x3
1 1 NA NA
2 1 NA NA
3 0 NA NA
4 0 NA NA
5 NA NA NA
Would appreciate if someone could spot the error in the code. Thanks.
tmp1
? For me,as.data.frame(lapply(tmp, recode.var))
anddplyr::mutate_all(tmp, recode.var)
give exactly what I think it is you're looking for. – duckmayr