Use existing functions and data through packages

[This article was first published on Quantargo Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Packages give you access to a huge set of functions and datasets, most of which are provided by the generous R community. They are the secret sauce which makes it possible to use R for pretty much anything you can imagine. Additionally, lots of packages are open source which can be a great learning resource.

  • Get to know the concept of packages in R
  • Learn how to call functions from packages
library(___)
data(___)

Introduction to packages

Packages are one of the best things in R. They add new functions and features to the language environment and extend its applications over many different use cases and domains. Packages are supported by a large community of developers and allow R to connect to many different external algorithms and libraries—many of them even written in different programming languages.

Contributors all over the world including developers or domain experts in physics, finance, statistics etc. create a lot of additional content, such as custom functions for specific use cases. These functions, together with documentation, help files and datasets can be gathered into packages. Packages can be made public through package repositories so that anyone can install and use them. The most popular package repository is CRAN which hosts over 15,000 packages.

Calling a package

As a demonstration we will use the generate_primes() function from the primes package. This function takes two numbers as parameters and outputs all prime numbers inside their range.

In order to use a package we first need to load it. This can be done by applying the library() function and inserting the name of the package as the first argument of the function. After that, we have access to all of the content in the package and can use functions from it as usual.

library(primes)
generate_primes(min = 500, max = 550)
[1] 503 509 521 523 541 547

Exercise: Check for leap year

  1. Load the lubridate package.
  2. Use the leap_year function to check if 2020 is leap year or not. (Hint: the function takes the year in the form of a number as the first parameter date )
Start Exercise

Use existing functions and data through packages is an excerpt from the course Introduction to R, which is available for free on quantargo.com

VIEW FULL COURSE

To leave a comment for the author, please follow the link and comment on their blog: Quantargo Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)