Working with Lubridate Package in R

This tutorial gives you an introduction about working with the lubridate package in R.

This post is partly based on the following paper (PDF) published by the authors of the lubridate package:

Garrett Grolemund and Hadley Wickham, Dates and Times Made Easy with lubridate, Journal of Statistical Software, Vol 40, No. 3, April 2011

Read more Working with Lubridate Package in R

Introduction to Dates and Times in R

This tutorial gives a brief introduction to dates and times in R.

1  Overview

There are several ways of handling with date and time data in R:

  • The date class in base R — handles dates without time
  • The chron package — handles dates and times without control for time zones.
  • The POSIXt classes in base R — handle dates and times with control for time zone.
  • The lubridate package
  • The timeDate package
  • The zoo package
  • The xts package

General rule for choosing ways for handling dates/times:

  • I would choose date and POSIXt classes if they meet my requirements, since they are included in R base. Why bother with external (package) dependencies unless very necessary?

Read more Introduction to Dates and Times in R

Write R Output to Text File

This tutorial presents two ways to write R output to text file.

R provides two functions for writing objects to files in ASCII format:

  1. write() function — suitable for the same kind of data as read using scan() function;
  2. write.table() function — suitable for the types of data which would normally be read using read.table() function.

Note, the write.fwf() function in the gdata package can write R objects to a file using fixed-width fields, but it is not covered here due to its non-common usage.

Read more Write R Output to Text File

Generating and Working with Sequence Data in R

This tutorial covers some key points about generating and working with sequence data in R.

1  Generating sequence data

Generating vectors / sequences of data is commonly used for simulations or coding tests when ‘real’ data isn’t available.

1.1 Sequences

1.1.1  seq()

1.1.2  gl()

The gl() function can be used to generate factors by specifying the pattern of their levels. Read more Generating and Working with Sequence Data in R

Import Data into R from External File

This tutorial covers several ways to import data into R from external file

1  Import using scan() function

The scan() function is most appropriatee when all the data to be read is of same mode, so that it can be accommodated by a vector or matrix.

1.1  Argument — file

  • A quoted string or character variable containing the name of a file, or a URL, or a connection.
  • If file=”” or the argument omitted, then scan() will read from console, stopping when a completely blank line is input, for example, hit ENTER without typing anything.

Read more Import Data into R from External File