R: Remove constant and identical features programmatically
[This article was first published on R – The Hack-R Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
##### Removing constant features cat("n## Removing the constants features.n") for (f in names(train)) { if (length(unique(train[[f]])) == 1) { cat(f, "is constant in train. We delete it.n") train[[f]] <- NULL test[[f]] <- NULL } } ##### Removing identical features features_pair <- combn(names(train), 2, simplify = F) toRemove <- c() for(pair in features_pair) { f1 <- pair[1] f2 <- pair[2] if (!(f1 %in% toRemove) & !(f2 %in% toRemove)) { if (all(train[[f1]] == train[[f2]])) { cat(f1, "and", f2, "are equals.n") toRemove <- c(toRemove, f2) } } }
To leave a comment for the author, please follow the link and comment on their blog: R – The Hack-R Blog.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.