Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Use dplyr's _if() functions like mutate_if() with a negative predicate function

$
0
0

According to the documentation of the dplyr package:

# The _if() variants apply a predicate function (a function that
# returns TRUE or FALSE) to determine the relevant subset of
# columns.
# mutate_if() is particularly useful for transforming variables from
# one type to another
iris %>% mutate_if(is.factor, as.character)

So how do I use the inverse form? I would like to transform all non-numeric values to characters, so I thought of doing:

iris %>% mutate_if(!is.numeric, as.character)
#> Error in !is.numeric : invalid argument type

But that doesn't work. Or just select all variables that are not numeric:

iris %>% select_if(!is.numeric)
#> Error in !is.numeric : invalid argument type

Doesn't work either.

How do I use negation with dplyr functions like mutate_if(), select_if() and arrange_if()?


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>