Rainar Angelo

View Original

Day 1: What's a package and how do you use one?

Hello 

Congratulations on taking the time out to learn R program.

Before we get started, I assume it's the end of your day as you're reading it.

You're probably tired, yet, here you are putting in the effort to learn something new.

I'd recommend:

1. Grabbing a notebook or opening a word doc 

2. Opening RStudio

3. A Glass of Water

4. Silencing your notifications

Alright! Let's dive in

Here's what we'll learn today:

  • Common programmer lingo

  • The RStudio Console

  • What are packages - installing and loading them

Why do we need to know common programmer lingo?

Why? Because, just yesterday, I made a mistake when referring to code as "a code". Before the guardians of the programmer world death stare me (or you) into oblivion, here's a small takeaway:

It's not "a code"

It's a "piece" of code

That being said, here are some common terms to keep in mind.

  • Snippet:
    "Here's a code snippet" or "I'll share a snippet" for a small, self-contained piece of code.

  • Block:
    "This code block handles user authentication" for a distinct section of code with a specific purpose.

  • Function:
    "I've written a function to calculate the average" for a specific, named piece of functionality.

  • Script:
    "The entire script is about 100 lines long" for a complete file of code.

  • Library/Package:
    "We're using the pandas library for data manipulation" for a collection of pre-written code that can be imported and used.

So the next time you want to use the right word, you have this list handy 😉

Now on to the meatier stuff

Once you open RStudio, you'll see 4 different panes.

Our primary focus today will be on the 'Console' pane.

You'll type the code in the 'Console' pane and press 'Enter' to run it (or CTRL + Enter)

The rest? I'll introduce them to you as we make use of them

Packages - and installing them

The most common way to introduce yourself to R is by 'importing' a file.

While it's 'important', 

Importing, tidying and transforming data are 'less' interesting thereby making the subject 'boring' to start with.

That's why our first focus will be on 'visualisation'.

A common (and useful) package for doing so in R is 'ggplot2'

gg stands for 'grammar of graphics'

(bet no one has told you that before 😂)

How do you use ggplot?

  1. You need to install it

  2. (This applies to any package you want to use. It has to be installed for the first time)

  3. You need to load it

  4. (You need to load a package everytime you start a new session)

How do you install a package?

Let's try installing tidyverse.

Now, tidyverse loads 9 packages:

dplyr, forcats, ggplot2, lubridate, purrr, readr, stringr, tibble, and tidyr.

Installing it is easy. 

All you have to do is run the code:

install.packages("tidyverse")

and hit enter

But where do you enter the code?

You'll type the code in the 'Console' pane and press 'Enter' to run it (or CTRL + Enter)

Give it some time. You'll see several lines of output. This'll include download progress, installation progress etc.

Here's what it'll look like when you're done 👆
To load the package run the code:

library(tidyverse) and hit enter

Similarly, we can load other libraries once we've installed them.

All right!

If you're new to R, that may be a lot for today.

If you're not, then this might be something you could simply skim through.

However, we'll pick up the pace as we get introduced to datasets and graphs (something we'll look at tomorrow)

p.s.

if you have any questions on today's mail, feel free to reply to this email. I always respond (though I might take a while)

Hope you learned something today.

See you tomorrow!