Skip to content
Back to formatted view

Raw Message

Message-ID: <6948c3e3-b683-4700-ad2f-801fe9b335d3@charite.de>
Date: 2023-10-26T07:30:58Z
From: Berger, Hilmar
Subject: Description of base::levels()

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]]