purrr :: map和dplyr :: rename或dplyr:select重命名多个列

时间:2018-08-13 21:19:12

标签: r dplyr purrr

目标是在列表中包含的多个小标题中重命名(一长列)多个列。

作为单个小节上的管道,此代码可正常运行以重命名多列。

demo <- mt2 %>% dplyr::select(MPGS= 'mpg', DRATS = 'drat',everything())




mt<-as.tibble(mtcars)
mt2<-as.tibble(mtcars)
names(mt)<- c("unrelated", "different","column", "names",   "to", "further",      "confuse", "my"  , "code" ,  "purrrrrr", "cat")
master <- tibble( datatype = c("mt","mt2","mt"))
master <- master %>% mutate(data = purrr::map(datatype, function(x) get(x)))
test_change <- function(data){
  data <- data %>% 
    purrr::map(~ dplyr::rename_all(., funs(paste0("new",":",.)))) %>% 
    purrr::map(~ dplyr::select(.,
                          MPGS = "new:mpg",
                          DRATS = "new:drat",
                          everything()))
  data
}
test_change(master$data)

错误消息是

  

x:y中的错误:NA / NaN参数

0 个答案:

没有答案