Skip to content
Prev 377456 / 398502 Next

Help with factor column replacement value issue

As usual, careful reading of the relevant Help page would resolve the confusion.

from ?factor:

"factor(x, exclude = NULL) applied to a factor without NAs is a
no-operation unless there are unused levels: in that case, a factor
with the reduced level set is returned. If exclude is used, since R
version 3.4.0, excluding non-existing character levels is equivalent
to excluding nothing, and when excludeis a character vector, that is
applied to the levels of x. Alternatively, excludecan be factor with
the same level set as x and will exclude the levels present in
exclude."

In, subsetting a factor does not change the levels attribute, even if
some levels are not present. One must explicitly remove them, e.g.:
## 3 levels, all present
[1] a b
Levels: a b c
## 3 levels, but one empty
[1] a b
Levels: a b
## Now only two levels


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Fri, Nov 16, 2018 at 7:38 AM Bill Poling <Bill.Poling at zelis.com> wrote:
Message-ID: <CAGxFJbSVQH0Fmyx4D+rQjBERPDxxduvDwfet2AJtw5+zWhBa8Q@mail.gmail.com>
In-Reply-To: <BN7PR02MB50730DC2CE20AA5282EF53C3EADD0@BN7PR02MB5073.namprd02.prod.outlook.com>