tomw
tomw

Reputation: 3160

modify_if on a data frame

I'm not grokking something fundamental in the purrr::modify_if syntax.

Say I want to do something silly--take any variable with a name 2 characters long, and cut it.

here's my attempt:

library(tidyverse)
library(stringr)
library(magrittr)

mtcars %>% 
   modify_if(~. %>% 
          names %>%
          str_length %>%
          equals(2),
      function(i)
         cut_number(i, n = 2))

How do I pass a set of logical predictates in the first argument?

Upvotes: 1

Views: 606

Answers (1)

tomw
tomw

Reputation: 3160

as @thelatemail indicates, the problem is that the first function which returns the logical predicate needs to see the name attribute. fixing this works:

mtcars %>% 
   modify_if(mtcars %>% 
      names %>% 
      str_length %>% 
      equals(2),
   function(i) 
      i %>% 
      cut_interval(n = 2))

Upvotes: 1

Related Questions