I'm trying to create a function that looks up price and car type from a data set. Both will have default arguments. For price, this is easy enough. But for the car type (which I have as factors), I can't find a way to set all factors as default.
The goal is that if you don't set anything in car_type, it will return all possible car types.
search <- function(start_price = 0, end_price = 1000, car_type = ???){
subset_data <- auto[price <= end_price &
price > start_price &
vehicleType == car_type]
return(subset_data)
}
search()
So that the "search()" returns all cars between the prices of 0 and 1000 and of all possible car types. I've tried using vectors and lists, without any luck.
The usual way to approach this is to use NULL
as a default and handle that in the function.
search <- function(start_price = 0, end_price = 1000, car_type = NULL){
if (is.null(car_type) {
car_type <- levels(auto$vehicleType)
}
subset_data <- auto[price <= end_price &
price > start_price &
vehicleType %in% car_type]
return(subset_data)
}