0

I have a data set for which I'd like to rename all the columns using a list of characters of the same length.

Here is my code at the moment, which tries to use the rename command.


eurrefcolnames = c('euRefVoteW1','euRefVoteW2', 'euRefVoteW3', 'euRefVoteW4', 'euRefVoteW6','euRefVoteW7', 'euRefVoteW8',  'euRefVoteW9')


times <- in_all_waves %>% 
    filter(Paper == 'Times') %>%  
    ungroup()  %>%    
    select(-Paper) %>%
    map_df(table) %>% 
    t %>%
    as_tibble() %>%
    rename(eurrefcolnames = names(times))

But this doesn't seem to work. The function replace_with requires a function and it seems silly to have to write a function that just calls a list. Is there a simpler way I'm not seeing ?

Here is the table I'm using for context (taken after the as_tibble) command:

structure(list(V1 = c(316L, 157L, 2L, 56L), V2 = c(290L, 123L, 
3L, 51L), V3 = c(313L, 159L, 3L, 55L), V4 = c(324L, 154L, 3L, 
50L), V5 = c(338L, 134L, 2L, 57L), V6 = c(320L, 189L, 2L, 20L
), V7 = c(325L, 187L, 2L, 17L), V8 = c(335L, 181L, 0L, 0L)), row.names = c(NA, 
-4L), class = c("tbl_df", "tbl", "data.frame"))
jolene
  • 373
  • 2
  • 15
  • 2
    Does this answer your question? [Rename multiple columns by names](https://stackoverflow.com/questions/20987295/rename-multiple-columns-by-names) – latlio Jan 06 '21 at 17:44

1 Answers1

1

You can use set_names from the purrr package:

mydf %>%
 purrr::set_names(eurrefcolnames)
Cettt
  • 11,460
  • 7
  • 35
  • 58