Add a prefix to column names

后端 未结 4 817
情深已故
情深已故 2020-11-28 03:52

When reading the following helpfile it should be possible to add a prefix to the column names :

colnames(x, do.NULL = TRUE, prefix = \"col\")
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 04:45

    I will add a tidyverse approach to this problem, for which you can both add suffix and prefix to all column names. The following adds a prefix in a dplyr pipe.

    df <- data.frame(x = c(1, 2), y = c(3, 4))
    df %>% dplyr::rename_all(function(x) paste0("a", x))
    

    Adding suffix is easier.

    df %>% dplyr::rename_all(paste0, "a")
    

提交回复
热议问题