The Switch Operator in R by Guangming Lang ~1 min read November 21, 2014 Categories r The switch() function in R is very powerful and here’s an example. center = function(x, type) { # Finds the center of x # # Args: # x: numeric vector # type: string specifying center type # # Returns: # the specified center of x switch(type, mean = mean(x), median = median(x), trimmed = mean(x, trim=0.1)) } # unit test x = rcauchy(10) center(x, "mean") ## [1] -1.237765 center(x, "median") ## [1] -0.9514988 center(x, "trimmed") ## [1] -1.082965 Share Tweet LinkedIn Reddit Previous Calculate Tangency Portfolios Using R Next How to Scrape Japanese Text Using the rvest Package