Open jaapwalhout opened 5 years ago
could you try on current master? there was just a big change related to assign that affected factor levels as well that may have fixed this? my laptop is running some heavy stuff at the moment or I'd gladly check
At least the behaviour seems consistent with the help text for :=
:
When
LHS
is a factor column andRHS
is a character vector with items missing from the factor levels, the new level(s) are automatically added (by reference, efficiently), unlike base methods.
@MichaelChirico Would like to, but currently not in the situation I can.
Thx @Henrik-P, I should have checked.
As it is thus intended behavior, it would be appropriate (imho) to give a warning instead of silently updating the factor levels. Now it can catch people by surprise and can therefore have unintended consequences (as it did for me).
IMO raising warning is not really good solution. This is a feature over data.frame way, well documented for long time. So if suddenly we will start to raise warning about it I don't think it will be nice for users, that rely on it. And I can imagine that more people rely on that convenient feature.
Best way might be not to add a wrapper that checks all(values %chin% levels(col))
before attempting to assign.
Consider the following example data:
Now when I want to change the 3rd value of
x
indf
toC
, I will get an error with the dataframe:However when I want to this with the datable using
:=
to update, there is no error-message (or a warning) andC
is added as a factor level.To me this is unexpected behavior. I expected an error like when using a dataframe. When this is intended behavior, a warning would be appropriate.
Possibly related to #2403
Session info: