Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(plyr)
- mat = t(replicate(3,sample(as.character(1:3),100,replace=T)))
- DF = data.frame(mat, stringsAsFactors = FALSE)
- DF = colwise(factor, levels = as.character(1:3))(DT)
- mode_f = function(v){
- tv <- tabulate(v)
- if (sum(tv == max(tv)) > 1){
- NA
- } else {
- levels(v)[which.max(tv)]
- }
- }
- new_sample = as.character(colwise(mode_f)(DF))
- na_loc = which(new_sample == "NA")
- for (j in na_loc){
- distance = rowSums(sweep(as.matrix(DF[,1:(j-1)]), 2, new_sample[1:(j-1)], '=='))
- if (sum(distance == min(distance)) > 1){
- new_sample[j] = DF[sample(which(distance == min(distance)), 1),j]
- } else {
- new_sample[j] = DF[which.min(distance),j]
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement