Reputation: 785
i want to rename columns and keep the rest. My code works but it creates a weird error
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)
}
This is the error Note: Using an external vector in selections is ambiguous.
Upvotes: 0
Views: 54
Reputation: 886938
If we need to rename the columns use rename
with !!!
(according to documentation - ?"!!!"
- The big-bang operator !!! forces-splice a list of objects. The elements of the list are spliced in place, meaning that they each become one single argument.)
as list.of.names
is a named vector
library(dplyr)
student_performance1 <- student_performance %>%
rename(!!! list.of.names)
student_performance1
-output
# 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>
-checking the names of the original data
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"
-transformed data column names
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"
Using the function
columns2newname <- function(dataset,
new.names.to.old.names){
dataset %>%
rename(!!! new.names.to.old.names)
}
columns2newname(student_performance, list.of.names)
Upvotes: 1