1

I have a .csv file with many variables, and I want to analyse this data using R. Many of the columns are categorical in nature, but encoded as integers. I would like to change the type of these columns to be a factor (or an ordered factor, depending).

While I know how to change the type of a single column (asked several times, for example Converting variables to factors in R), I have a lot of variables (200, say), and it's very annoying to change their types separately by hand. From the name of the column, I can instantaneously decide whether the column is numeric or not.

Is there a way to easily change the column types of a large number of variables (not all to the same type)?

The ideal solution would be interactive, with a single keystroke per variable. I'm using RStudio, so if that has some built-in GUI for easier column type editing, that would be helpful. I'm looking for a UI, really.

There is another question already about changing the type of many columns simultaneously (Change the class of many columns in a data frame). Here, it is explained how to change a range of columns to the same data type. This doesn't help in my case, where I need to decide for every column separately what the right type is.

Community
  • 1
  • 1
Daan Michiels
  • 160
  • 1
  • 7
  • 1
    [this](http://stackoverflow.com/questions/3796266/change-the-class-of-many-columns-in-a-data-frame) looks related. – Imran Ali Oct 09 '16 at 03:55
  • @ImranAli - Thanks. However, that question asks to change a range of columns to factors. Not all of my columns need to be changed to factors, but rather I have to decide on a column-by-column basis what the right type is. – Daan Michiels Oct 09 '16 at 14:39
  • I'm not aware of an RStudio built-in GUI for easier column type editing. The [Data Viewer](https://support.rstudio.com/hc/en-us/articles/205175388-Using-the-Data-Viewer) may be of some help. Or, you should look for an add-in such as [GREA](https://www.r-bloggers.com/grea-the-rstudio-add-in-to-read-all-the-data-into-r/) – Uwe Oct 09 '16 at 14:42

0 Answers0