Fairly new R user here, just now getting into more advanced material. I am writing a simple function that utilizes the arrange function in the dplyr package. The function intends to filter a dataframe using partial string matching and then sort the result on a column specified by the 'sortby' argument in the function. Here is what I have thus far:
calcs <- function(data, type, sortby) {
filt <- data[grep(type, data$name), ]
ord <- dplyr::arrange(filt, desc(sortby))
return(ord)
}
The sortby variable is not being found when evaluated. I am speculating that it has something to do with standard and non-standard evaluation, but this is a bit outside my programming aptitude.
Here is a reproducible example:
mydata <- data.frame(name=c("GI Joe", "GI Joe", "Batman", "Xbox"),
rating=c(8,12,8,1))
calcs(data=mydata, type="GI", sortby=rating)
Error in desc(sortby) : object 'rating' not found
To be clear, my goal with this function is to filter a dataset using partial string matching and to sort the result on a column of interest.