Applying dplyr's rename to all columns while using pipe operator

前端 未结 5 1285

I\'m working with an imported data set that corresponds to the extract below:

set.seed(1)
dta <- data.frame(\"This is Column One\" = runif(n = 10),
               


        
5条回答
  •  时光说笑
    2021-01-04 03:29

    You can also try this

    set.seed(1)
    dta <- data.frame("This is Column One" = runif(n = 10),
                     "Another amazing Column name" = runif(n = 10),
                     "!## This Columns is so special€€€" = runif(n = 10),
                    check.names = FALSE)
    
    dta <- dta  %>% 
      setNames(gsub("[^[:alnum:] ]", perl = TRUE,
                "",
                names(.))) %>% 
      setNames(gsub("(\\w)(\\w*)",
                "\\U\\1\\L\\2",
                perl = TRUE,
                names(.)))
    
    names(dta)
    [1] "This Is Column One"          "Another Amazing Column Name" " This Columns Is So Special"
    

提交回复
热议问题