Skip to content
Back to formatted view

Raw Message

Message-ID: <CAMk+s2Q8Ue4pCiFk9yEhW9D+CPedvas0ZPjmdsB=Hu1R3U=58w@mail.gmail.com>
Date: 2021-09-19T08:17:51Z
From: Luigi Marongiu
Subject: how to remove factors from whole dataframe?

Hello,
I woul dlike to remove factors from all the columns of a dataframe.
I can do it n a column at the time with
```

df <- data.frame(region=factor(c('A', 'B', 'C', 'D', 'E')),
                 sales = c(13, 16, 22, 27, 34), country=factor(c('a',
'b', 'c', 'd', 'e')))

new_df$region <- droplevels(new_df$region)
```

What is the syntax to remove all factors at once (from all columns)?
For this does not work:
```
> str(df)
'data.frame': 5 obs. of  3 variables:
 $ region : Factor w/ 5 levels "A","B","C","D",..: 1 2 3 4 5
 $ sales  : num  13 16 22 27 34
 $ country: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5
> df = droplevels(df)
> str(df)
'data.frame': 5 obs. of  3 variables:
 $ region : Factor w/ 5 levels "A","B","C","D",..: 1 2 3 4 5
 $ sales  : num  13 16 22 27 34
 $ country: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5
```
Thank you