I have following matrix.
r1 <- c("M","A","T","D","T","Y")
r2 <- c("M","A","G","G","D", "J")
r3 <- c("M","B","H","G","T", "Y")
r4 <- c("M","B","G","G","X", "Y")
r5<- c("F","A","H","D","T", "Y")
n.mat <- rbind(r1,r2,r3,r4,r5)
n.mat<-as.data.frame(n.mat)
I would like to replace values in columns with only two values by the most frequent value (for each column). And leave columns with more than two unique values as they are.
Output:
r1 <- c("M","A","T","G","T","Y")
r2 <- c("M","A","G","G","D", "Y")
r3 <- c("M","A","H","G","T", "Y")
r4 <- c("M","A","G","G","X", "Y")
r5<- c("M","A","H","G","T", "Y")
n.mat <- rbind(r1,r2,r3,r4,r5)
n.mat<-as.data.frame(n.mat)