1
votes

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)

2
It works fine with me.Nareman Darwish
@NaremanDarwish Yeah, I am not sure what's going on. Raised issue on GitHub: github.com/r-lib/tidyselect/issues/160. Maybe Lionel knows what's going on.Indrajeet Patil
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
@NelsonGon The linked issue also contains session info.Indrajeet Patil
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

2 Answers

3
votes

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..
2
votes

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)