0

I have made this function that optionally subsets a dataframe:

irisSubset <- function(dataframe, Subset=FALSE){

  df <- dataframe

  if(Subset != FALSE) {
    df <- subset(df, Species %in% Subset)
  }

  df

}


irisSubset(iris, Subset = c("setosa"))
irisSubset(iris, Subset = c("setosa", "versicolor"))
irisSubset(iris)

The function does what I want it to do. However, I realise I have fed a vector to if(), which causes a warning message. Are there any circumstances in which the above function will not work correctly? Is there a way of improving the function and avoiding the warning message?

4

0 回答 0