8. R#

R logo

The DJ Turntable for Data Wizards 🎧🔼

If Python is your universal gaming console, consider R your DJ Turntable for mastering data magic and statistics. Just like a DJ mixes and remixes songs to create auditory experiences, you can use R to mix and remix data to tell compelling stories and make ground-breaking discoveries.

  • Type: Specialized language for data manipulation, statistical analysis, and visualization.

  • Primary Uses: Academic research, data analytics, statistical modeling, data visualization

  • Paradigms: Functional, Object-Oriented

  • Community: A diverse following within academia and business analytics, which makes it a rich platform for statistical methodologies and open-source packages.

Scratch, Mix, and Remix: Data Tunes

Picture this: Each dataset is like a vinyl record. You can spin it, scratch it, mix it, and remix it to discover new rhythms and patterns. Want to run a linear regression? That’s like setting up your base track. How about adding a layer of complex statistical models? Now, you’re adding in beats and loops. All of these are done seamlessly with packages like ggplot2 for visualization, dplyr for data manipulation, and tidyr for data cleaning.

Collaborative Mixtape with Python, Stata, and R

Ever attended a concert where different DJs come together to mix and remix tracks, creating an auditory journey that takes you through different musical genres? That’s how you can perceive working with Python, Stata, and R in the Google Colab environment. You can blend different data languages to create your data “mixtape,” turning your research into a collaborative and interactive masterpiece.

Data Visualization: The Light Show 🌈

What’s a DJ performance without a spectacular light show? In the world of R, visualizations are your light show, illuminating insights and helping your audience ‘see the music.’ With ggplot2, you can create multi-layered visuals that are as expressive as they are informative.

Drop the Beat: Real-time Collaboration đŸŽ€

Just as DJs can livestream their sets and collaborate in real-time, you can do the same in the world of data with R. Share your R notebooks in real-time, invite others to join your data party, and crowd-source solutions and ideas. With platforms like RStudio Cloud and Rpubs, you’re never DJing alone.

From Garage DJ to Festival Headliner 🌟

Everyone starts somewhere—maybe mixing tunes in a garage or coding simple bar plots. But with dedication and practice, you can go from simple plots to intricate visual stories and from basic statistics to complex machine learning models. Just like DJs work their way up to headline music festivals, you can become a ‘headliner’ in the world of data science.


Quizzes and Hands-on Exercises 📝

R Libraries Quiz:

Question: Which R package is famous for its visualization capabilities?

  • A) ggplot2

  • B) plyr

  • C) shiny

  • D) lubridate

Answer: A) ggplot2

Hands-on Exercise with R:

  1. Creating Plots with R and ggplot2

Objective: Use ggplot2 to create a scatter plot showing the relationship between variables in a dataset.

Steps:

  1. Open RStudio or Google Colab with R support.

  2. Type the following code snippet in the script or notebook.

Code Block:

library(ggplot2)
data(mpg)
ggplot(mpg, aes(x=displ, y=hwy)) +
  geom_point(aes(color=class)) +
  ggtitle("Highway miles per gallon vs. Engine Displacement") +
  xlab("Engine Displacement (liters)") +
  ylab("Highway miles per gallon")
  1. Creating a Shiny App

Objective: Build a simple Shiny app to display data.

Steps:

  1. Open RStudio.

  2. Install and load the Shiny package.

  3. Type and execute the following code snippet.

Code Block:

library(shiny)

ui <- fluidPage(
  titlePanel("My First Shiny App"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("obs", "Number of observations:", 1, 100, 50)
    ),
    mainPanel(
      plotOutput("distPlot")
    )
  )
)

server <- function(input, output) {
  output$distPlot <- renderPlot({
    hist(rnorm(input$obs))
  })
}

shinyApp(ui, server)

Become the DJ of your data. Mix, remix, and let the data dance! đŸŽ¶đŸ“ŠđŸŽ‰

R Code
# Loading libraries
library(ggplot2)
library(gganimate)

# Defining parameters for a particle in a magnetic field
t <- seq(0, 10, by = 0.1)
x <- sin(t)
y <- cos(t)

# Creating a data frame to hold the values
particle_data <- data.frame(
  time = t,
  x = x,
  y = y
)

# Creating the plot using ggplot2 and gganimate
plot <- ggplot(particle_data, aes(x = x, y = y)) +
  geom_path(aes(group = 1), color = 'gray') + # Making sure all points are in the same group
  geom_point(size = 4) +
  labs(title = "Particle in a Magnetic Field",
       x = "X Axis",
       y = "Y Axis") +
  theme_minimal() +
  coord_fixed(ratio = 1) +
  transition_reveal(time)

# Create and save the animation
animate(plot, renderer = gifski_renderer("particle_animation.gif"))

As you run this and visualize the “Particle in a Magnetic Field”, remember, this stunning visual narrative is orchestrated not from a standalone R environment but from the cohesive .ipynb platform (which produces everythin think in this platform), showcasing the beauty of integrated tools.

R is not just a language; it’s a call to create, innovate, and explore. In a world where data-driven stories resonate, let’s harmonize our tools and craft tales that inspire. 🌠đŸ§Ș📈


R in a Jupyter Env

Installing the R kernel for Jupyter notebooks allows you to run R code within the Jupyter environment. Below are the steps to install the R kernel for Jupyter notebooks:

Option 1: Using Anaconda (Recommended for Beginners)

  1. Download and Install Anaconda: If you don’t have Anaconda installed, you can download and install it from the official site.

  2. Open Anaconda Navigator: Once installed, open the Anaconda Navigator software.

  3. Install R and RStudio: Go to the Environments tab and create a new environment, choosing R as the language. This will also install RStudio by default.

  4. Install R Kernel in Jupyter: Open a terminal from Anaconda Navigator and run the following command to install the R kernel:

    conda install -c r r-irkernel
    
  5. Open Jupyter Notebook: Now, if you start Jupyter Notebook, you should be able to create a new notebook with R.

Option 2: Using the Command Line

  1. Install R: If you haven’t installed R yet, download and install it from CRAN.

  2. Install R Kernel: Open an R terminal and run the following commands to install the R kernel:

    install.packages('IRkernel')
    IRkernel::installspec(user = FALSE)
    
  3. Restart Jupyter Notebook: If you already have a Jupyter Notebook session running, you’ll need to restart it.

Option 3: Using a Package Manager (e.g., Homebrew for macOS)

  1. Install R: Use Homebrew to install R:

    brew install R
    
  2. Install R Kernel: As in Option 2, open an R terminal and install the IRkernel package.

Now, when you open a new Jupyter Notebook, you should see the option to create a notebook using the R kernel.

That’s it! You should now be able to use Jupyter Notebooks for your R programming tasks.

Load Packages
# Install and load a package
install_and_load <- function(package_name) {
  if (!require(package_name, character.only = TRUE, quietly = TRUE)) {
    install.packages(package_name)
  }
  suppressPackageStartupMessages(library(package_name, character.only = TRUE, quietly = TRUE, warn.conflicts = FALSE))
}

# List of packages to install and load
packages <- c("icdpicr", "dplyr", "readr", "tidyr")

# Suppress warnings and install/load each package
suppressWarnings({
  for (pkg in packages) {
    install_and_load(pkg)
  }
})
Attaching package: ‘dplyr’
The following objects are masked from ‘package:stats’:

    filter, lag
The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union