0

I would like to be able to identify columns in a dataframe (eg cols am and vs in mtcars ) that contain integers and convert them to factors.

Important note: In the dataset, columns that do contain integers, are encoded as numeric (so is.integer would not work)..

Is there a solution with tidyverse? Thank you!

EDIT: I found the solution in this post: check if all values in data.frame columns are integers to subset dummy variables aka are all values in a column TRUE?

tzema
  • 451
  • 3
  • 11
  • `mpg %>% mutate(across(where(is.integer), factor))`. There could be dupes for this – akrun Dec 29 '21 at 19:08
  • That could be it, however some of the columns that do contain integers are encoded as "numeric". – tzema Dec 29 '21 at 19:09
  • Yes, in the `mpg`, there is only one column that is `dbl` (numeric) - displ. So, if you do `is.integer`, it should correctly select only integer columns i.e. `mpg %>% select(where(is.integer))` If you want both numeric as well, use `is.numeric` which select both integer and double `mpg %>% mutate(across(where(is.numeric), factor))` – akrun Dec 29 '21 at 19:12
  • 2
    I am sorry, I had to mention mtcars, instead of mpg. I found the solution in this post: https://stackoverflow.com/questions/60549597/check-if-all-values-in-data-frame-columns-are-integers-to-subset-dummy-variables – tzema Dec 29 '21 at 19:16
  • 1
    Can i tag this as duplicate – akrun Dec 29 '21 at 19:21
  • Sure. Do you want me to delete it, or should I let it be, in case anyone else has a similar question? – tzema Dec 29 '21 at 19:22
  • 1
    Not really. dupe tagging helps in finding the post more easily for others – akrun Dec 29 '21 at 19:25
  • 1
    Does this answer your question? [check if all values in data.frame columns are integers to subset dummy variables aka are all values in a column TRUE?](https://stackoverflow.com/questions/60549597/check-if-all-values-in-data-frame-columns-are-integers-to-subset-dummy-variables) – Col Bates - collynomial Jan 04 '22 at 11:19

0 Answers0