Data Science Classroom Series – 10/Dec/2021

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 Preview

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 Preview
    • using c function to create sequence of numbers and all alphabets Preview
    • 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 Preview
      • If we have a vector with integer, logical and numeric Preview
      • IF we have a vector with integer, logical Preview
  • 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 Preview Preview Preview
  • 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 Preview
    • Examples Preview Preview
    • Lets give names to the rows and columns Preview Preview
    • Lets create a marks matrix Preview

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin