I have following matrix with numbers of 0 and 1 containing always the same number of strings per column. The minimum number of strings in one column is 2. I would like to delete columns when they fulfill both conditions together.
- containing only two strings (
10and01), - and if
01occurs only one or two times. But I would like to leave all the other columns as they are:
r1 <- c("10","001","0001","01","100","10")
r2 <- c("01","001","0001","10","100","10")
r3 <- c("10","100","1000","10","010","01")
r4 <- c("10","010","0100","10","001","10")
r5<- c("01","010","0010","10","001","10")
r6<- c("01","010","0010","10","001","01")
n.mat <- rbind(r1,r2,r3,r4,r5,r6)
The output:
r1 <- c("10","001","0001","100")
r2 <- c("01","001","0001","100")
r3 <- c("10","100","1000","010")
r4 <- c("10","010","0100","001")
r5<- c("01","010","0010","001")
r6<- c("01","010","0010","001")
n.mat <- rbind(r1,r2,r3,r4,r5,r6)
Removing column 4 and 6.
My code so far is:
del_two<- function(x){
length(unique(x)) != 2
}
msa_protein.mat_1<-msa_protein.mat[, apply(msa_protein.mat, 2, del_two)]
But I am not so sure how to add the if function.