I would like to write a function that sorts by a column. This elementary exercise has of course been asked many times before, but the solutions suggested either depend on the literal and hence cannot be used in a function (also here, there, and there), or require dependence on the column ordering, which makes for brittle programming (also here).
What I'm looking for has apparently been dubbed "referential transparency". Fine. But it appears that adopting this term, at least in the present example, would lead to using very many words to define and distinguish literals in a program. Hence an MWE is best.
What should the body of the function sort.by.column
contain, so that
sort.by.column <- function(df, column.name) {
## ??
}
df1 <- data.frame(Instrument=c("B","A"),
Value=c(3,2))
df2 <- data.frame(Device=c("D","C"),
Value=c(5,4))
column.name.1 <- "Instrument"
sorted1 <- sort.by.column(df1, column.name.1)
column.name.2 <- "Device"
sorted2 <- sort.by.column(df2, column.name.2)
will work for both df1
and df2
?