Skip to content
Prev 389260 / 398506 Next

how to remove factors from whole dataframe?

On Sun, 19 Sep 2021 10:17:51 +0200
Luigi Marongiu <marongiu.luigi at gmail.com> wrote:

            
What on earth do you mean by that?  After struggling with your
(inadequate) example for a while, I conjecture that what you want to do
is to drop unused levels from all factor columns in a data frame.

I is that correct?
Before executing the foregoing command, you would have to create
new_df.  *Perhaps* you intended to do "new_df <- df" initially.

If this is the case, then new_df will be exactly the same as df
after you've applied droplevels() to new_df$region.

Note that droplevels() removes unused levels from the levels of a
factor.  The factor df$region in your confusing example has no unused
levels, so droplevels() has no effect upon it.
I believe the reason you think "this does not work" is that your
example is inadequate.  If the factors in "df" actually had any unused
levels, then droplevels(df) would indeed remove them.

(a) In future please present your questions in a comprehensible manner.

(b) Also please construct your examples so that they are actually
capable of illustrating what you a trying to accomplish.

You are asking others for help.  Have a little consideration for the
helpers, who are giving of their time and effort free of charge!

(c) Note that "df" is a lousy name for a data frame, since it is the
name of a base R function (the density function for the F distribution).
No harm is done in the current context, but such nomenclature can at
times lead to errors "object of type 'closure' is not subsettable"
which mystifies most users.

cheers,

Rolf Turner