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

后端 未结 5 1715
花落未央
花落未央 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:47

    The latest solution (2020) seems to use rename_with, which is available in dplyr 1.0.0 and higher:

    mtcars %>% rename_with(.fn = ~ paste0("Myprefix_", .x, "_Mypostfix")) -> mtcars.custom
    

    Use the .cols = argument to specify a subset of variables, it defaults to everything().

提交回复
热议问题