Hi All ,
Trying to write a function that takes multiple and variable arguments (Column names) then orders the data frame based on selected columns.
Works:
colSort1 <- function(data,col1,col2){
ord<-order(data[,col1],data[,col2],decreasing=TRUE)
sorted <-data[ord,]
invisible(sorted)
}
test1<-tcolSort1(mtcars,"disp","hp")
Does not work
colSort <-function(data,...,decreasing=TRUE){
l1<-list(...)
ord<-order(glue::glue("data","[,{l1}]"))
sorted <-data[ord]
invisible(sorted)
}
t1<-colSort(mtcars,"disp","hp")
The dplyr alternative could be arrange and all_of but trying it get a hang of ( ...) in functions.
Thanks