I have a below data frame and I want to check binary columns and change non-empty value to 1.
a <- c("","a","a","","a")
b <- c("","b","b","b","b")
c <- c("c","","","","c")
d <- c("b","a","","c","d")
dt <- data.frame(a,b,c,d)
I am able to get the solution by looping and traversing through each column. But, I want some efficient solution because my data frame is really really large and the below solution is way much slower.
My Solution-
for(i in 1:length(colnames(dt)))
{
if(length(table(dt[,i]))==2){
dt[which(dt[,i]!=""),i] <- 1
}
}
Expected Output:
a b c d
1 b
1 1 a
1 1
1 c
1 1 1 d
Is there a way to make it more efficient.
ncharnotlength. Do you want to replace the empty values withNA,0, or something else? (It would really help if you provided your expected output.)length(unique(dt[, 1])) == 2)will probably be faster thantable(). If, as in your sample data, your columns are already factors you could do a little better reassigning thelevels.