[Rd] Description of base::levels()

Berger, Hilmar h||m@r@berger @end|ng |rom ch@r|te@de
Thu Oct 26 09:30:58 CEST 2023


Dear list,

I have a doubt/suggestion to change the help page of  base::levels().

in the examples section, one reads (R 4.3.1):

## we can add levels this way:
f <- factor(c("a","b"))
levels(f) <- c("c", "a", "b")
f

[1] c a
Levels: c a b

f <- factor(c("a","b"))
levels(f) <- list(C = "C", A = "a", B = "b")
f


[1] A B
Levels: C A B


However, none of the examples only adds a level - the first replaces level "a" with "c", "b" with "a" and then adds the new level "b", while the second replaces levels a and b with their upper-case versions and adds level C.

This might lead to additional confusion since levels() <-  behaves quite differently in this aspect from the levels argument of factor(), e.g.

f1 <- factor(c("a","b"))

f1 <- factor(f1, levels = c("c","a","b") )

f1

[1] a b
Levels: c a b

would indeed add a new level "c" while maintaining the existing ones.

I would suggest to at least replace the "add levels" in the comment by "modify levels".

Does this make sense?

Best regards

Hilmar


	[[alternative HTML version deleted]]



More information about the R-devel mailing list