How can we mutate an new column name containing an existing value

112 views Asked by At

The median of mpg is 19.2.

In the next line I would like to add this info (e.g. "19.2") to the new mutated column name "median_dich". So that at the end the new column name should be median_dich19.2.

I know how to do it outside of this special situation. My question is to directly get the median value and paste it to the new mutated column name?

What I have tried so far:

#TRY 1
library(dplyr)
mtcars %>% 
  mutate(median_mpg = median(mpg)) %>% 
  mutate(glue("median_dich{median_mpg}") = ifelse(mpg > median_mpg, 1, 0))

#TRY 2
library(dplyr)
library(rlang)

mtcars %>%
  mutate(median_mpg = median(mpg)) %>%
  mutate(!!paste0("median_dich_", median_mpg) := ifelse(mpg > median_mpg, 1, 0))

#TRY 3
library(dplyr)
library(rlang)
library(glue)

mtcars %>%
  mutate(median_mpg = median(mpg)) %>%
  mutate(!!sym(glue("median_dich_{median_mpg}")) := ifelse(mpg > median_mpg, 1, 0))
3

There are 3 answers

2
Mark On BEST ANSWER

A one-liner:

mtcars %>% mutate("median_dich{median(.$mpg)}" := mpg > median(mpg))

You can wrap mpg > median(mpg) in as.integer(), but it makes little/no difference, and adds visual clutter, so I didn't.

Here's a longer version, where we create the median_mpg column too:

mtcars %>% 
  mutate(median_mpg = median(mpg)) %>%
  mutate("median_dich{.$median_mpg[1]}" := ifelse(mpg > median_mpg, 1, 0))

Output (with relevant columns):

                     mpg median_dich19.2
Mazda RX4           21.0               1
Mazda RX4 Wag       21.0               1
Datsun 710          22.8               1
Hornet 4 Drive      21.4               1
Hornet Sportabout   18.7               0
Valiant             18.1               0
Duster 360          14.3               0
Merc 240D           24.4               1
Merc 230            22.8               1
Merc 280            19.2               0
Merc 280C           17.8               0
Merc 450SE          16.4               0
Merc 450SL          17.3               0
Merc 450SLC         15.2               0
Cadillac Fleetwood  10.4               0
Lincoln Continental 10.4               0
Chrysler Imperial   14.7               0
Fiat 128            32.4               1
Honda Civic         30.4               1
Toyota Corolla      33.9               1
Toyota Corona       21.5               1
Dodge Challenger    15.5               0
AMC Javelin         15.2               0
Camaro Z28          13.3               0
Pontiac Firebird    19.2               0
Fiat X1-9           27.3               1
Porsche 914-2       26.0               1
Lotus Europa        30.4               1
Ford Pantera L      15.8               0
Ferrari Dino        19.7               1
Maserati Bora       15.0               0
Volvo 142E          21.4               1

Note: I'm sure I don't need to tell you this TarJae, but putting data in your column names is bad practice- I can't think off the top of my head of a situation where it would be a good idea...so consider not doing it! ^^

0
L Tyrone On

Convoluted, but does this suit your needs? I couldn't think of an inline substitution method that worked, so resorted to tidyr:

library(dplyr)
library(tidyr)
library(tibble)

mtcars %>% 
  mutate(temp = rownames(.),
  median_mpg = median(mpg),
  median_dich = ifelse(mpg > median_mpg, 1, 0)) %>%
  pivot_longer(median_dich) %>%
  mutate(name = paste0(name, median_mpg)) %>%
  pivot_wider(names_from = name,
              values_from = value) %>%
  column_to_rownames("temp")
0
Nikita Vladimirov On

Please try this code, maybe it will solve your problem

library(tidyverse)
data <- mtcars %>% 
   mutate(median_mpg = median(mpg)) %>%
   set_names(c(names(.[,-ncol(.)]),paste0("median_dich_",unique(.[,ncol(.)]))))