I'm trying to create a function that will order dataframe columns according to a specific column name order that I have as a vector.
I have tried:
order_df <- function(df, order_names){
df <- df[, order_names]
return(df)
}
and also
order_df <- function(df) {
df <- df[, order_names]
return(df)
}
where order_names is something like c("A", "C", "B", "D")
and A
,B
,C
,D
are column names.
They both give the mistake: Error: object 'df' not found
I essentially want it to do this: Sort columns of a dataframe by column name
But in a function.
Thank you in advance