How do I add a prefix to several variable names using dplyr?

后端 未结 5 1714
花落未央
花落未央 2020-12-10 03:06

I\'m trying to add a common prefix to each of the variable names in a data.frame. For example, using the mtcars data, I could add the prefix \"cars.\" using th

5条回答
  •  误落风尘
    2020-12-10 03:29

    Indeed, you can use rename_ (NSE rename itself doesn’t work):

    data %>% rename_(.dots = setNames(names(.), paste0('cars.', names(.))))
    

    … but honestly, why? Just assigning names directly is shorter and more readable:

    data %>% setNames(paste0('cars.', names(.)))
    

提交回复
热议问题