如何重命名列并保留其余列?

时间:2020-10-25 19:55:45

标签: r

我想重命名列并保留其余部分。我的代码有效,但是会产生奇怪的错误

student_performance <-
  read_csv("https://raw.githubusercontent.com/UBC-MDS/ellognea-smwatts-student-performance/master/data/student-math-perf.csv") %>% 
  as_tibble()

list.of.names <-
  c("student_age" = "age",
  "parental_status" = "Pstatus")


columns2newname <- function(dataset,
                            new.names.to.old.names){
  df <-
    dataset %>% 
      select(new.names.to.old.names, everything())  

  return(df)
  
}

这是错误Note: Using an external vector in selections is ambiguous.

1 个答案:

答案 0 :(得分:1)

如果我们需要重命名列,请使用rename!!!(根据文档-?"!!!"-大爆炸运算符!!!强制拼接对象列表。列表中的元素被拼接到位,这意味着它们每个都成为一个单独的参数。)

list.of.names是命名为vector

library(dplyr)
student_performance1 <- student_performance %>%
       rename(!!! list.of.names)
student_performance1

-输出

# A tibble: 395 x 33
#   school sex   student_age address famsize parental_status  Medu  Fedu Mjob  Fjob  reason guardian traveltime studytime
#   <chr>  <chr>       <dbl> <chr>   <chr>   <chr>           <dbl> <dbl> <chr> <chr> <chr>  <chr>         <dbl>     <dbl>
# 1 GP     F              18 U       GT3     A                   4     4 at_h… teac… course mother            2         2
# 2 GP     F              17 U       GT3     T                   1     1 at_h… other course father            1         2
# 3 GP     F              15 U       LE3     T                   1     1 at_h… other other  mother            1         2
# 4 GP     F              15 U       GT3     T                   4     2 heal… serv… home   mother            1         3
# 5 GP     F              16 U       GT3     T                   3     3 other other home   father            1         2
# 6 GP     M              16 U       LE3     T                   4     3 serv… other reput… mother            1         2
# 7 GP     M              16 U       LE3     T                   2     2 other other home   mother            1         2
# 8 GP     F              17 U       GT3     A                   4     4 other teac… home   mother            2         2
# 9 GP     M              15 U       LE3     A                   3     2 serv… other home   mother            1         2
#10 GP     M              15 U       GT3     T                   3     4 other other home   mother            1         2
# … with 385 more rows, and 19 more variables: failures <dbl>, schoolsup <chr>, famsup <chr>, paid <chr>,
#   activities <chr>, nursery <chr>, higher <chr>, internet <chr>, romantic <chr>, famrel <dbl>, freetime <dbl>,
#   goout <dbl>, Dalc <dbl>, Walc <dbl>, health <dbl>, absences <dbl>, G1 <dbl>, G2 <dbl>, G3 <dbl>

-检查原始数据的名称

names(student_performance)
#[1] "school"     "sex"        "age"        "address"    "famsize"    "Pstatus"    "Medu"       "Fedu"       "Mjob"      
#[10] "Fjob"       "reason"     "guardian"   "traveltime" "studytime"  "failures"   "schoolsup"  "famsup"     "paid"      
#[19] "activities" "nursery"    "higher"     "internet"   "romantic"   "famrel"     "freetime"   "goout"      "Dalc"      
#[28] "Walc"       "health"     "absences"   "G1"         "G2"         "G3"       

转换后的数据列名称

names(student_performance1)
#[1] "school"          "sex"             "student_age"     "address"         "famsize"         "parental_status"
#[7] "Medu"            "Fedu"            "Mjob"            "Fjob"            "reason"          "guardian"       
#[13] "traveltime"      "studytime"       "failures"        "schoolsup"       "famsup"          "paid"           
#[19] "activities"      "nursery"         "higher"          "internet"        "romantic"        "famrel"         
#[25] "freetime"        "goout"           "Dalc"            "Walc"            "health"          "absences"       
#[31] "G1"              "G2"              "G3"          

使用功能

columns2newname <- function(dataset,
                        new.names.to.old.names){

   dataset %>% 
        rename(!!! new.names.to.old.names)



 }

columns2newname(student_performance, list.of.names)