Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mutate variable if column name contains a string

Tags:

r

dplyr

My data has several columns which contain the string "trait". I'd like to mutate these columns in the same way using dplyr.

My first instinct was to use mutate_if in the following way:

my_data %>% mutate_if(contains('trait'), funs(if_else(.=='True',T,F)) )

But I get the error

Error: Variable context not set

What does that error mean? How can I properly use the mutate_if function?

here is a sample of my data (with some other columns ommited).

tibble::tribble(
  ~X1_on_1_rush_trait, ~acrobatic_clearance_trait, ~argues_with_officials_trait, ~avoids_using_weaker_foot_trait, ~backs_into_player_trait, ~bicycle_kicks_trait, ~cautious_with_crosses_trait, ~chip_shot_trait, ~chipped_penalty_trait, ~comes_for_crosses_trait, ~corner_specialist_trait, ~diver_trait, ~dives_into_tackles_trait, ~diving_header_trait, ~driven_pass_trait, ~early_crosser_trait, ~fan.s_favourite_trait, ~fancy_flicks_trait, ~finesse_shot_trait, ~flair_trait, ~flair_passes_trait, ~gk_flat_kick_trait, ~gk_long_throw_trait, ~gk_up_for_corners_trait, ~giant_throw_in_trait, ~inflexible_trait, ~injury_free_trait, ~injury_prone_trait, ~leadership_trait, ~long_passer_trait, ~long_shot_taker_trait, ~long_throw_in_trait, ~one_club_player_trait, ~outside_foot_shot_trait, ~playmaker_trait, ~power_free_kick_trait, ~power_header_trait, ~puncher_trait, ~rushes_out_of_goal_trait, ~saves_with_feet_trait, ~second_wind_trait, ~selfish_trait, ~skilled_dribbling_trait, ~stutter_penalty_trait, ~swerve_pass_trait, ~takes_finesse_free_kicks_trait, ~target_forward_trait, ~team_player_trait, ~technical_dribbler_trait, ~tries_to_beat_defensive_line_trait,                ~name,
              "False",                    "False",                      "False",                         "False",                  "False",              "False",                      "False",          "False",                "False",                  "False",                  "False",      "False",                   "False",              "False",            "False",              "False",                "False",             "False",             "False",       "True",             "False",             "False",              "False",                  "False",               "False",           "False",            "False",             "False",           "False",            "False",                 "True",              "False",                "False",                  "False",          "False",                 "True",             "False",        "False",                   "False",                "False",            "False",        "False",                   "True",                "False",            "False",                         "False",               "False",            "False",                   "False",                             "False",  "Cristiano Ronaldo",
              "False",                    "False",                      "False",                         "False",                  "False",              "False",                      "False",           "True",                "False",                  "False",                  "False",      "False",                   "False",              "False",            "False",              "False",                "False",             "False",              "True",      "False",             "False",             "False",              "False",                  "False",               "False",           "False",            "False",             "False",           "False",            "False",                 "True",              "False",                 "True",                  "False",           "True",                "False",             "False",        "False",                   "False",                "False",            "False",        "False",                   "True",                "False",            "False",                         "False",               "False",            "False",                   "False",                             "False",           "L. Messi",
              "False",                    "False",                      "False",                         "False",                  "False",              "False",                      "False",          "False",                "False",                  "False",                  "False",       "True",                   "False",              "False",            "False",              "False",                "False",             "False",             "False",       "True",             "False",             "False",              "False",                  "False",               "False",           "False",            "False",             "False",           "False",            "False",                "False",              "False",                "False",                  "False",          "False",                "False",             "False",        "False",                   "False",                "False",            "False",        "False",                   "True",                "False",            "False",                          "True",               "False",            "False",                    "True",                             "False",             "Neymar",
              "False",                    "False",                      "False",                         "False",                  "False",              "False",                      "False",          "False",                "False",                  "False",                  "False",       "True",                   "False",              "False",            "False",              "False",                "False",             "False",             "False",      "False",             "False",             "False",              "False",                  "False",               "False",           "False",            "False",             "False",           "False",            "False",                "False",              "False",                "False",                  "False",          "False",                "False",             "False",        "False",                   "False",                "False",            "False",        "False",                  "False",                "False",            "False",                         "False",               "False",            "False",                    "True",                              "True",          "L. Suárez",
               "True",                    "False",                      "False",                         "False",                  "False",              "False",                      "False",          "False",                "False",                   "True",                  "False",      "False",                   "False",              "False",            "False",              "False",                "False",             "False",             "False",      "False",             "False",             "False",               "True",                  "False",               "False",           "False",            "False",             "False",           "False",            "False",                "False",              "False",                "False",                  "False",          "False",                "False",             "False",        "False",                    "True",                "False",            "False",        "False",                  "False",                "False",            "False",                         "False",               "False",            "False",                   "False",                             "False",           "M. Neuer",
              "False",                    "False",                      "False",                         "False",                  "False",              "False",                      "False",           "True",                "False",                  "False",                  "False",      "False",                   "False",              "False",            "False",              "False",                "False",             "False",              "True",      "False",             "False",             "False",              "False",                  "False",               "False",           "False",             "True",             "False",           "False",            "False",                "False",              "False",                "False",                  "False",          "False",                "False",             "False",        "False",                   "False",                "False",            "False",        "False",                  "False",                "False",            "False",                         "False",               "False",            "False",                   "False",                             "False",     "R. Lewandowski"
  )
like image 339
Demetri Pananos Avatar asked Jan 27 '18 18:01

Demetri Pananos


1 Answers

You should wrap your contains("trait") variable filter into vars() call

my_data %>% 
  mutate_at(vars(contains('trait')), funs(.=='True'))

P.S. I suggest you also drop your if_else() call and just use logical comparison directly

like image 104
Sergej Andrejev Avatar answered Oct 03 '22 08:10

Sergej Andrejev