What is the difference between drop.levels (x) in gdata package and as.factor (as.character (x))?

as a question, since I see that the speed is higher for a later method, why use the first? Thanks.

+4
source share
3 answers

Two teams do not quite the same, but not quite, especially if you have the original order of factors. In some cases, you cannot use: as.factor(as.character(f)) . Cm:

 par(mfrow=c(2,3)) f <- factor(c("D", "B", "C", "K", "A"), levels=c("K", "B", "C", "D"))[2:4] plot(f, main="Original factor") f.fc <- as.factor(as.character(f)) plot(f.fc, main="as.factor(as.character(f))") fd <- drop.levels(f) plot(fd, main="drop.levels(f)") fd <- drop.levels(f, reorder=FALSE) plot(fd, main="drop.levels(f, reorder=FALSE))") ff <- factor(f) plot(ff, main="factor(f)") 

alt text

as.factor(as.character(f)) and drop.levels(f) do the same, and they don’t preserve the original order of the factors, they both redefine the text in ABC order. I want to keep the order in which you can use the reorder=FALSE option in drop.levels() .

This is the default behavior of factor() .

+2
source

If you are trying to remove unused levels, all you have to do is:

 x <- factor(x) 
+2
source

New to R (from version 2.12.0) is the droplevels() function to do the same. It is implemented as:

 > base:::droplevels.factor function (x, ...) factor(x) <environment: namespace:base> 

Therefore, I would use this function out of preference. This is a common function in R with methods for objects of the "factor" and "data.frame" , the latter is useful when there are many factors in the data frame that require lowering levels.

+2
source

Source: https://habr.com/ru/post/1335800/


All Articles