1

I already read Use dynamic variable names in `dplyr` but my problem is to mutate an already existing column with a column name stored as a string variable.

data <- tibble::tribble(
  ~colA, ~colB,
  1, 2,
  3, 4
)

namestring <- "colA"

----

> data
# A tibble: 2 x 2
   colA  colB
  <dbl> <dbl>
1     1     2
2     3     4

> namestring
[1] "colA"

Now I want to modify the colA using namestring

Wanted output without namestring is like this.

data %>%
  dplyr::mutate(colA = colB * 100)

## A tibble: 2 x 2
#   colA  colB
#  <dbl> <dbl>
#1   200     2
#2   400     4

But it gives me error using dplyr programming

namestring <- "colA"
data %>%
  dplyr::mutate(.data[[namestring]] = colB + 2)

# Error: unexpected '=' in:
# "data %>%
#   dplyr::mutate(.data[[namestring]] ="

filter and select don't produce same kind of error using .data[[string]] on LHS

data %>%
  dplyr::filter(.data[[namestring]] == 3)

## A tibble: 1 x 2
#   colA  colB
#  <dbl> <dbl>
#1     3     4
alperceire
  • 45
  • 1
  • 7

1 Answers1

3

We can use := with !!

library(dplyr)
data %>% 
    mutate(!! namestring := colB * 100)

-output

# A tibble: 2 x 2
#   colA  colB
#  <dbl> <dbl>
#1   200     2
#2   400     4    

Or it can be within across

data %>%
    mutate(across(all_of(namestring), ~ colB* 100))
# A tibble: 2 x 2
#   colA  colB
#  <dbl> <dbl>
#1   200     2
#2   400     4
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    @alperceire I showed two options. The first one is when there is a single column and you want to modify it. Second with `across` is more general as it can take more than one or more columns – akrun Jan 08 '21 at 21:52