2.10 End of Chapter Exercise
Data Conversion
Convert the following character string to numeric:
- 1,234,567 (comma as decimal mark)
- 1.234.566 (full stop as decimal mark)
Convert the following character string to date:
- Jan-21-2004
- 01-21-2004
- Jaunary-21-04
Vector
Create a vector of integers from 5 to 99.
Create a vector of five zeros.
Create a vectors of dates from January 1st, 2014 to January 5th, 2014.
Factor
Using a numeric vector x = c(1,2,3,3,5,5,6,7,8,9,9,10):
create a factor vector that divides the data into three groups of the same lenght: High, Middle, and Low. (Hint: Use the function cut)
create a factor vector that divides the data into three groups: Above average, between average and lower quartile, and below lower quartile. (Hint: Use the function cut and quartile)
Vector
- Create two new numeric vectors x and y.
- Create a new vector \(z = x + y\).
Matrix
- Create two new numeric vectors x and y.
- Create a dataframe from two newly created vectors.
- Create a new column in the dataframe that \(w = x - 2 \times y\).
Function
- Write the following function:
- take three numerical numbers,
- calculate the product of three numerical inputs,
- display the value, and
- return the value as function output.
- Write the following function:
- take three numerical numbers, but the last input has default value of 1,
- calculate the sum of the first two numerical inputs, and then the result is multipled by the third input,
- display the value, and
- return the value as function output.
Packages
- Install package quantmod.
- Load the package quantmod.