rdplyr

How to rename the target of a mutate statement


I'm trying to rename the results of a mutate on the fly but it's not working. Here is the sample dataset. There is also an environment variable, meas.num that has the value 2 in this case.

  meas.name    measure  FISE
  <chr>          <dbl> <dbl>
1 nl_netops_km   -4.88  2.17
2 nl_netops_km    8.93  1.46
3 nl_netops_km   -1.97  0.78
4 nl_netops_km   10.3   1.86
5 nl_netops_km    2.14  2.08
6 nl_netops_km    0.5   0.81
7 nl_netops_trust    4.38  1.84
8 nl_netops_trust   10.6   1.87
9 nl_netops_trust    4.38  1.84
10 nl_netops_trust   10.6   1.87
11 nl_netops_trust    4.38  1.84
12 nl_netops_trust   10.6   1.87

I want to get an additional variable called frac.meas_nl_netops_km (for the first 6 cases), and frac.meas_nl_netops_trust (for the second 6) with the values measure/meas.num.

Here's one try. I take the result from mutate and try assigning the variable to a different name.

lev1test <- lev1test2 %>%
    select(meas.name, measure, FISE) %>%
    mutate(frac.meas = measure / meas.num) 

    assign(paste0("frac.meas_", lev1test2$meas.name), lev1test2$frac.meas)
In assign(paste0("frac.meas_", lev1uni.0$meas.name), lev1uni.0$frac.meas) :
  only the first element is used as variable name

Here's another try. Here I use rename applied to frac.meas from the previous mutute

lev1test <- lev1est2 %>%
    select(meas.name, measure, FISE) %>%
    mutate(frac.meas = measure / meas.num) %>%
    rename(as.name(paste0("frac.meas_", meas.name))=frac.meas)

the message is

Error: unexpected '=' 

One more. I try to rename the variable in the middle of the mutate statement.

lev1test <- lev1est2 %>%
    select(meas.name, measure, FISE) %>%
    mutate(as.name(paste0("frac.meas_", meas.name)) = measure / meas.num

This gives the message:

Error: unexpected '=' in:
"    select(meas.name, measure, FISE) %>%
    mutate(as.name(paste0("frac.meas_", meas.name)) ="

Does anyone have a hint or solution or advice? Thanks.


Solution

  • Not sure if I've misunderstood, but is this your desired output?

    library(tidyverse)
    
    lev1test2 <- read.table(text = "meas.name    measure  FISE
    nl_netops_km   -4.88  2.17
    nl_netops_km    8.93  1.46
    nl_netops_km   -1.97  0.78
    nl_netops_km   10.3   1.86
    nl_netops_km    2.14  2.08
    nl_netops_km    0.5   0.81
    nl_netops_trust    4.38  1.84
    nl_netops_trust   10.6   1.87
    nl_netops_trust    4.38  1.84
    nl_netops_trust   10.6   1.87
    nl_netops_trust    4.38  1.84
    nl_netops_trust   10.6   1.87", header = TRUE)
    
    meas.num <- 2
    
    lev1test <- lev1test2 %>%
      mutate(frac.meas = measure / meas.num,
             ID_col = meas.name) %>%
      pivot_wider(id_cols = c(meas.name, measure, FISE),
                  names_from = ID_col,
                  values_from = frac.meas,
                  values_fn = list) %>%
      unnest(cols = everything()) %>%
      rename_with(.cols = -c(meas.name, measure, FISE), .fn = ~str_c("frac.meas_", .x))
    lev1test
    #> # A tibble: 12 × 5
    #>    meas.name       measure  FISE frac.meas_nl_netops_km frac.meas_nl_netops_tr…¹
    #>    <chr>             <dbl> <dbl>                  <dbl>                    <dbl>
    #>  1 nl_netops_km      -4.88  2.17                 -2.44                     NA   
    #>  2 nl_netops_km       8.93  1.46                  4.46                     NA   
    #>  3 nl_netops_km      -1.97  0.78                 -0.985                    NA   
    #>  4 nl_netops_km      10.3   1.86                  5.15                     NA   
    #>  5 nl_netops_km       2.14  2.08                  1.07                     NA   
    #>  6 nl_netops_km       0.5   0.81                  0.25                     NA   
    #>  7 nl_netops_trust    4.38  1.84                 NA                         2.19
    #>  8 nl_netops_trust    4.38  1.84                 NA                         2.19
    #>  9 nl_netops_trust    4.38  1.84                 NA                         2.19
    #> 10 nl_netops_trust   10.6   1.87                 NA                         5.3 
    #> 11 nl_netops_trust   10.6   1.87                 NA                         5.3 
    #> 12 nl_netops_trust   10.6   1.87                 NA                         5.3 
    #> # ℹ abbreviated name: ¹​frac.meas_nl_netops_trust
    

    Created on 2025-05-16 with reprex v2.1.1