Help to write my own select method for selecting any variables from any dataset by their names,
without using dplyr. How to correctly specify method argument for choosing variables?
It works in one line df[, c('year', 'month')]
, but not in a function.
select <- function(dataset, vars) {
dataset[, c(vars)]
}
select(df, 'year', 'month')
CodePudding user response:
You specified more arguments that the fcuntion expected. Just wrap your column names in a vector c()
and pass it to the function.
select_func <- function(dataset, vars) {
dataset[vars] # or dataset[, vars]
}
select_func(iris, c("Sepal.Length", "Species"))