coding_heart - 2 months ago 8

R Question

I've got a set of variables that are characters that I am trying to convert into a binary by creating the following function and using the

`apply()`

`a <- as.factor(c("n/a", "False", "False", "True"))`

b <- as.factor(c("n/a", "True", "False", "True"))

y <- data.frame(a,b)

conv <- function(x){

levels(x)[which(levels(x)=="n/a")] <- NA

levels(x)[which(levels(x)=="False")] <- 0

levels(x)[which(levels(x)=="True")] <- 1

x <- as.numeric(levels(x))[x]

return(x)

}

apply(y,2, conv)

However, when I do this, it outputs NAs. Alternatively, if you apply the function by column, it works:

`conv(y[,1])`

conv(y[,2])

The expected output should be:

`y:`

NA NA

0 1

0 0

1 1

Any thoughts on why this is happening? Thanks.

Answer

Your function is fine you just need to use `lapply`

.

```
conv <- function(x){
levels(x)[which(levels(x)=="n/a")] <- NA
levels(x)[which(levels(x)=="False")] <- 0
levels(x)[which(levels(x)=="True")] <- 1
x <- as.numeric(levels(x))[x]
return(x)
}
lapply(y,conv)
```

Also if the order of levels is same for all the variables then you could just do this.

```
conv <- function(x){
levels(x)=c(0,NA,1)
return(x)
}
lapply(y, conv)
```