0

I would like to remove all integer columns from my dataframe utilizing dplyr leaving only non-integer numeric columns. I thought I would be able to do like with the below:

Note: I realize I can select the numerics in my example below, but my actual dataset has other variable types in addition to numeric and integer.

library(dplyr)
mydata <- USArrests

mydata2 <- mydata %>% 
  select(!is.integer)

but that gives the error:

Error in !is.integer : invalid argument type

mydata2 <- mydata %>% 
  select(-is.integer)

Errors out with:

Error in -x : invalid argument to unary operator

The following works, but I'd like to know how to do this in dplyr as well.

mydata_temp <- mydata[, sapply(mydata, function(x) !is.integer(x))]
mydata_numeric_vars <- mydata_temp %>% 
  select_if(is.numeric)

Any suggestions? Seems like this should be fairly straightforward in dplyr but in looking around stack I can't seem to find a similar Q/A for this?

Russ Thomas
  • 938
  • 4
  • 13
  • 23
  • Use `select_if(!is.integer)` – akrun Oct 06 '18 at 18:37
  • 1
    You should look here: [How to select non-numeric columns using dplyr::select_if](https://stackoverflow.com/questions/48430882/how-to-select-non-numeric-columns-using-dplyrselect-if) – tmfmnk Oct 06 '18 at 18:39

2 Answers2

3

a possible workaround:

mydata %>% 
  .[ , !sapply(., is.integer)]
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
2

Use the select_if

out <- mydata %>%
           select_if(Negate(is.integer))
str(out)
#'data.frame':  50 obs. of  2 variables:
# $ Murder: num  13.2 10 8.1 8.8 9 7.9 3.3 5.9 15.4 17.4 ...
# $ Rape  : num  21.2 44.5 31 19.5 40.6 38.7 11.1 15.8 31.9 25.8 ...

If we want to select more than one type, then use

mydata %>% 
   select_if(~ !(is.integer(.x)) | is.numeric(.x))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks @akrun, so is there a way to combine that in one select_if statement to select numerics too or does that require two select statements like: kiva_numeric_vars <- kiva %>% select_if(Negate(is.integer)) %>% select_if(is.numeric) – Russ Thomas Oct 06 '18 at 19:16
  • 1
    @RussThomas Try `mydata %>% select_if(~ !(is.integer(.x)) | is.numeric(.x))` – akrun Oct 06 '18 at 19:19
  • 1
    [This answer](https://stackoverflow.com/questions/51358388/how-to-select-if-in-dplyr-where-the-logical-condition-is-negated) by @astaines led me to this alternative as well: mydata %>% select_if(funs(!is.integer(.) & is.numeric(.))) %>% head() – Russ Thomas Oct 06 '18 at 19:38
  • 1
    @RussThomas You can either wrap it in `funs` or use `~` – akrun Oct 06 '18 at 19:38