1 of 22

Introduction to �R and RStudio

Mukesh Behera

2 of 22

What is R ???

  • Statistical Analysis
  • Graphics
  • Free
  • Open Source
  • Extension, Packeges
  • Community
  • Platform

3 of 22

Requirement to use R

  • Desktop/Laptop
  • Operation System
  • System Configuration
  • Internet

4 of 22

Installation of R

  • R Installation Link:
  • https://cran.r-project.org/

5 of 22

RStudio Installation

  • RStudio Installation:
  • https://posit.co/download/rstudio-desktop/

6 of 22

IDE for R

  • RStudio
  • Visual Studio Code
  • Jupyter Notebook
  • Eclipse
  • R-Commander
  • RKWard
  • Emacs & ESS

7 of 22

Example

  • 2 + 2= 4
  • 2*5 = 10
  • 25 / 5 = 5

8 of 22

Basic R Commands

# Arithmetic

2 + 3

5 * 4

6^2

# Variable assignment

x <- 10

y <- 5

z <- x + y

# Printing outputprint

(z)

9 of 22

Data Types in R

# Numeric:

x <- 10

# Character:

name <- "John"

# Logical:

flag <- TRUE

# Vectors:

v <- c(1, 2, 3)

# Data Frames:

data.frame(name, age)

10 of 22

Basic Data Manipulation

  • # Creating a vector
  • <- c(2, 4, 6, 8)
  • # Accessing elements
  • v[1]
  • # Creating a data frame
  • df <- data.frame(Name=c("Alice", "Bob"), Age=c(25, 30))
  • # Accessing columns
  • df$Name

11 of 22

Loading Data

  • # Read CSV file
  • data <- read.csv("data.csv")
  • # View the first few rows
  • head(data)
  • # Summary statistics
  • summary(data)

12 of 22

Plotting Basics

  • # Simple plot
  • X <- c(1,2,3,4)
  • y <- c(2,4,6,8)
  • plot(x, y, type="b", col="blue")

13 of 22

Variables and Operations

  • # Variable types
  • x <- 5# numeric
  • y <- "Hello" # character
  • z <- TRUE # logical
  • # Type checking
  • class(x)
  • is.numeric(x)
  • is.character(y)
  • # Type conversion
  • as.character(x)
  • as.numeric("10")

14 of 22

Vectors and Sequences

  • # Creating vectors
  • v1 <- c(1, 2, 3, 4)
  • v2 <- seq(1, 10, by=2)
  • v3 <- rep(5, times=3)
  • # Vector operations
  • v1 + 1
  • v1 * 2
  • v1 + v2
  • # Indexing
  • v1[2]
  • v1[-1]

15 of 22

Working with Data Frames

  • # Create a data frame
  • df <- data.frame(Name=c("Alice", "Bob"), Age=c(25, 30))
  • # Accessing columns
  • df$Name
  • df[1, ]
  • df[, "Age"]
  • # Adding new columns
  • df$Gender <- c("F", "M")

16 of 22

Conditional Statements

  • x <- 10
  • if (x > 5) {
  • print("x is greater than 5")
  • } else {
  • print("x is 5 or less")
  • }

17 of 22

Loops

  • # For loop
  • for (i in 1:5) {
  • print(i)
  • }
  • # While loop
  • x <- 1
  • while (x <= 5) {
  • print(x)
  • x <- x + 1
  • }

18 of 22

Plotting

  • # Histogram
  • hist(c(1,2,2,3,3,3,4,4,5), col="lightblue")
  • # Boxplot
  • boxplot(df$Age, main="Age Distribution", col="orange")
  • # Barplot
  • counts <- table(df$Gender)
  • barplot(counts, col=c("pink", "lightblue"))

19 of 22

Installing and Loading Packages

  • # Install a package (only once)
  • install.packages("ggplot2")
  • # Load a package
  • library(ggplot2)

20 of 22

Cleaning and Filtering Data (with dplyr)

  • # Load dplyr
  • library(dplyr)
  • # Filter row
  • sdf_filtered <- filter(df, Age > 25)
  • # Select columns
  • df_selected <- select(df, Name, Age)
  • # Add new column
  • df <- mutate(df, AgeNextYear = Age + 1)

21 of 22

Reference

22 of 22

Thank You