Visual Studio Coding (R)

   In this exercise, you will work with rivers data. The data pertain to the length of major North American rivers, and it is built-in data in R. You can access the data by typing: 

> rivers 

Your task is to build a function that returns descriptive statistics for different inputs. The function is named descriptive_function and has three arguments (i.e., another name of inputs). 

descriptive_function(list, char, num) 

The requirements of the function are as follows: 

· The function has three arguments. The first argument is a list of numbers in doubles. The second argument is a character, and the last argument is a number. 

· If char is ‘m ’, the function prints the arithmetic mean of the list. 

· If char is ‘v’, the function prints the variance of the list. 

· If char is ‘s’, the function prints the standard deviation of the list. 

· If char is ‘z’, the function prints the z-score of num with respect to the list. 

· You can use built-in R functions including mean(), var() and sd(). 

Sample test cases: 

> descriptive_function(rivers, ‘m’, 7) 

[1] 591.1844 

> descriptive_function(rivers, ‘m’) 

[1] 591.1844 

> descriptive_function(rivers, ‘s’) 

[1] 493.8708 

> descriptive_function(rivers, ‘v’, 8) 

[1] 243908.4 

> descriptive_function(rivers, ‘z’, 8) 

[1] -1.180844

Tags: No tags