1

I am trying to rename a few columns using dplyr::rename and tidyselect helpers to do so using some patterns.

How can I get this to work?

library(tidyverse)

# tidy output from broom (using development version)
(df <- broom::tidy(stats::oneway.test(formula = wt ~ cyl, data = mtcars)))

#> # A tibble: 1 x 5
#>   num.df den.df statistic   p.value method                                      
#>    <dbl>  <dbl>     <dbl>     <dbl> <chr>                                       
#> 1      2   19.0      20.2 0.0000196 One-way analysis of means (not assuming equ~

# renaming
df %>% 
  dplyr::rename(
  .data = .,
  parameter1 = dplyr::matches("^num"),
  parameter2 = dplyr::matches("^denom")
  )
#> Error: Column positions must be scalar

Created on 2020-01-12 by the reprex package (v0.3.0.9001)

Indrajeet Patil
  • 4,673
  • 2
  • 20
  • 51
  • 1
    It works fine with me. – Nareman Darwish Jan 12 '20 at 11:53
  • @NaremanDarwish Yeah, I am not sure what's going on. Raised issue on GitHub: https://github.com/r-lib/tidyselect/issues/160. Maybe Lionel knows what's going on. – Indrajeet Patil Jan 12 '20 at 12:19
  • Please add your `sessionInfo()` to see any differences between ours and your machine. It does work just fine for me(too). Linked issue contains a `traceback`, the former might be more useful(too).. – NelsonGon Jan 12 '20 at 13:25
  • 1
    @NelsonGon The linked issue also contains session info. – Indrajeet Patil Jan 12 '20 at 13:34
  • Ah, it was folded! The only difference with my session is that you're using a dev version of broom(judging by the 9000 at the end). Don't think that's important here though. – NelsonGon Jan 12 '20 at 13:34

2 Answers2

2

Your code works fine with me, however here are some other shorter ways that can help you and you can try;

library(tidyverse)

# tidy output from broom (using development version)
(df <- broom::tidy(stats::oneway.test(formula = wt ~ cyl, data = mtcars)))

#> # A tibble: 1 x 5
#>   num.df den.df statistic   p.value method                                      
#>    <dbl>  <dbl>     <dbl>     <dbl> <chr>                                       
#> 1      2   19.0      20.2 0.0000196 One-way analysis of means (not assuming equ~

# renaming
df %>% 
  rename(parameter1 = matches("^num"),
         parameter2 = matches("^denom"))

# # A tibble: 1 x 5
# parameter1 parameter2 statistic   p.value method                                               
# <dbl>      <dbl>     <dbl>     <dbl> <chr>                                                
#   1          2       19.0      20.2 0.0000196 One-way analysis of means (not assuming..

df %>% 
  rename(parameter1 = contains("num"),
         parameter2 = contains("denom"))

# # A tibble: 1 x 5
# parameter1 parameter2 statistic   p.value method                                               
# <dbl>      <dbl>     <dbl>     <dbl> <chr>                                                
#   1          2       19.0      20.2 0.0000196 One-way analysis of means (not assuming..

df %>% 
  rename(parameter1 = starts_with("num"),
         parameter2 = starts_with("denom"))

# # A tibble: 1 x 5
# parameter1 parameter2 statistic   p.value method                                               
# <dbl>      <dbl>     <dbl>     <dbl> <chr>                                                
#   1          2       19.0      20.2 0.0000196 One-way analysis of means (not assuming..
Nareman Darwish
  • 1,251
  • 7
  • 14
2

We can also rename from a named vector

library(dplyr)
library(stringr)
df %>% 
     rename(!!!set_names(names(df)[1:2], str_c('parameter', 1:2)))
# A tibble: 1 x 5
#  parameter1 parameter2 statistic   p.value method                                                  
#       <dbl>      <dbl>     <dbl>     <dbl> <chr>                                                   
#1          2       19.0      20.2 0.0000196 One-way analysis of means (not assuming equal variances)
akrun
  • 874,273
  • 37
  • 540
  • 662