If you want to run a unique data.frame file (for example, train.data), and also get counts (which can be used as weight in classifiers), you can do the following:
unique.count = function(train.data, all.numeric=FALSE) { # first convert each row in the data.frame to a string train.data.str = apply(train.data, 1, function(x) paste(x, collapse=',')) # use table to index and count the strings train.data.str.t = table(train.data.str) # get the unique data string from the row.names train.data.str.uniq = row.names(train.data.str.t) weight = as.numeric(train.data.str.t) # convert the unique data string to data.frame if (all.numeric) { train.data.uniq = as.data.frame(t(apply(cbind(train.data.str.uniq), 1, function(x) as.numeric(unlist(strsplit(x, split=",")))))) } else { train.data.uniq = as.data.frame(t(apply(cbind(train.data.str.uniq), 1, function(x) unlist(strsplit(x, split=","))))) } names(train.data.uniq) = names(train.data) list(data=train.data.uniq, weight=weight) }
user2771312 Sep 12 '13 at 5:47 on 2013-09-12 05:47
source share