Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to write my own select method for selecting any variables from any dataset by their names, without using dplyr

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')

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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"))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading