[R] how to remove factors from whole dataframe?

Luigi Marongiu m@rong|u@|u|g| @end|ng |rom gm@||@com
Sun Sep 19 10:17:51 CEST 2021


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



More information about the R-help mailing list