aggregate - Rename factor levels based on a condition in R -
i want combine factors count less n 1 factor named "else"
for example if n = 3 in following df want combine "c", "d" , "e" "else":
df = data.frame(x=c(1:10), y=c("a","a","a","b","b","b","c","d","d","e"))
i started out getting df low count values:
library(plyr) lowcounts = ddply(df, "y", function(z){if(nrow(z)<3) nrow(z) else null})
i know change these manually in practice have dozens of levels need automate this.
i want select , rename levels %in% lowcount in levels(df) , leave rest same not sure how proceed.
another alternative:
#your dataframe df = data.frame(x=c(1:10), y=c("a","a","a","b","b","b","c","d","d","e")) #which levels keep , change res <- table(df$y) notkeep <- names(res[res < 3]) keep <- names(res)[!names(res) %in% notkeep] names(keep) <- keep #set new levels levels(df$y) <- c(keep, list("else" = notkeep)) df # x y #1 1 #2 2 #3 3 #4 4 b #5 5 b #6 6 b #7 7 else #8 8 else #9 9 else #10 10 else
Comments
Post a Comment