Dates in R
- The builtin as.Date function handles dates (without times)
- The contributed library chron handles dates and times, but doesnot control for time zones.
- The POSIXct and POSIXlt classes allow for dates and times with control for time zones
- Internally Date objects are stored as number of days since January 1, 1970 (using negative numbers for earlier dates)
- POSIX represent portable operating system interface, primary for UNIX systems, but avaliable on other Operatings systems as well.
- Dates store in the POSIX format are date/time values, but also allow modification of time zones
- The two POSIX date/time calsses which differ in the way the values are internally stored. POSIXct class stores date/time values a number of seconds since January 1, 1970 while POSIXlt class stores as list with elements for hours, minutes, seconds, day, month and year etc.
- While you are using the Date function we have support of formats
Code | Value |
---|---|
%d | Day of the month (decimal number) |
%m | Month (decimal number) |
%b | Month (abbreviated) |
%B | Month (full Name) |
%y | Year (2 digit) |
%Y | Year (4 digit) |
- Samples
Data Structures
- There are few datastructures in R that are crucial to understand. These include
- Vectors
- Lists
- Matrices
- Data frames
- Vector:
- A vector is an object that holds collection of various data elements in R, though they are limited because every inside of vector must belong to the same variable type.
- To create a vector we will be using
c()
function - Creating basic vectors
- using c function to create sequence of numbers and all alphabets
- Coercion: In R we have a rule which says vectors should be data elements of same type
- Now if we give a vector with different types and one string/character, all the other elements will be converted to string/character
- If we have a vector with integer, logical and numeric
- IF we have a vector with integer, logical
- Now if we give a vector with different types and one string/character, all the other elements will be converted to string/character
- Lists:
- Lists are different from vector because it can hold many different types of R objects inside
- List is also collection of data elements with different types of data elements
- In R to create a list we use
list()
function - Samples
- Matrices:
- A Matrix is a two dimension vector with rows and columns.
- In R, one requirement for matrices is that every data element stored inside it should be of same type
- To create a matrices we will be using matrix() function
- hint: use help as shown below
- Examples
- Lets give names to the rows and columns
- Lets create a marks matrix