I need to select all columns that are not numeric. I can select all numeric columns easily using select_if
:
mtcars %>% select_if(is.numeric)
What if I want to select non-numeric
columns? I tried:
mtcars %>% select_if(!is.numeric)
But I got error message below:
Error in !is.numeric : invalid argument type
Thanks a lot for help!
We can use select_if() function to get numeric columns by calling the function with the dataframe name and isnumeric() function that will check for numeric columns.
In R, you can extract numeric and factor variables using sapply function.
To select columns that are only of numeric datatype from a Pandas DataFrame, call DataFrame. select_dtypes() method and pass np. number or 'number' as argument for include parameter.
You can use purrr
's negate()
which is included if you use library(tidyverse)
rather than just library(dplyr)
library(tidyverse) iris %>% select_if(negate(is.numeric))
You can use a purrr-style anonymous function, provided you have a reasonably recent version of dplyr:
library(dplyr) iris %>% select_if(~!is.numeric(.x)) %>% head() #> Species #> 1 setosa #> 2 setosa #> 3 setosa #> 4 setosa #> 5 setosa #> 6 setosa
or the old-style funs
notation still works, e.g.
iris %>% select_if(funs(!is.numeric(.))) %>% head() #> Species #> 1 setosa #> 2 setosa #> 3 setosa #> 4 setosa #> 5 setosa #> 6 setosa
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With