Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
20 views35 pages

R Programmimg Lab FIle

The document provides a comprehensive guide to R programming, covering installation, basic functionalities, data types, and various operations on data structures like vectors, lists, arrays, matrices, and data frames. It includes step-by-step instructions for installing R and RStudio, as well as detailed examples of R scripts demonstrating arithmetic, relational, and logical operations. Additionally, it explains how to visualize data and perform statistical analyses using built-in functions.

Uploaded by

Anshu Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views35 pages

R Programmimg Lab FIle

The document provides a comprehensive guide to R programming, covering installation, basic functionalities, data types, and various operations on data structures like vectors, lists, arrays, matrices, and data frames. It includes step-by-step instructions for installing R and RStudio, as well as detailed examples of R scripts demonstrating arithmetic, relational, and logical operations. Additionally, it explains how to visualize data and perform statistical analyses using built-in functions.

Uploaded by

Anshu Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

INDEX

SNO. PROGRAM SIGN


1 Introduction to R programming
2 Installing R and RStudio
3 List the R packages
4 Introduction to Basic functionality of R, variable, data types in R
5 Implement R script to show the usage of various operators available
in R language.
6 Implement R script to demonstrate all operations on vector and list
data structure
7
Implement R script to demonstrate all operations on array and
matrix data structure
8
Implement R script to demonstrate all operations on data frame and
factor data structure
9
Implement R script to show condition and decision-making
statements.
10 Implement different functions in R programming
11
Reading different types of data sets (.txt, .csv) from Web or disk
and writing in file in specific disk location and Reading Excel data
sheet in R.
12
Implement R Script to create a Pie chart, Bar Chart, scatter plot and
Histogram
13 Implement R Script to perform mean, median, mode, range,
summary, variance, standard deviation operations.
14
Implement R Script to perform Normal, Binomial distributions.
15
Implement R Script to perform correlation, Linear and multiple
regression.

Program 1:
Introduction to R Programming
1. What is R Programming?
R is a powerful programming language and environment designed for statistical computing,
data analysis, and data visualization. It is widely used by statisticians, data scientists, and
researchers for analyzing large datasets and creating predictive models.
 Key Features:
o Open-source and free
o Rich set of packages for statistical analysis
o Excellent for data visualization
o Supports integration with other languages like Python and C++

2. Basic Components of R
 Variables: Used to store data. Example: x <- 10 assigns the value 10 to the variable x.
 Data Types:
o Numeric: Integers and floating-point numbers (e.g., 25, 3.14)
o Character: Text strings (e.g., "Hello")
o Logical: Boolean values (TRUE or FALSE)
o Complex: Complex numbers (e.g., 3 + 2i)

3. Operators in R
 Arithmetic Operators: + (addition), - (subtraction), * (multiplication), / (division), ^
(exponentiation)
 Relational Operators: == (equal), != (not equal), < (less than), > (greater than), <=, >=
 Logical Operators: & (AND), | (OR), ! (NOT)
 Assignment Operator: <- (commonly used to assign values to variables)

4. Basic Functions in R
R provides many built-in functions to perform calculations and data manipulation:
 sum(): Adds numbers
 mean(): Calculates average
 sqrt(): Calculates square root
 abs(): Returns absolute value
 max(), min(): Find maximum and minimum values

5. Conditional Statements
Conditional statements allow decision-making in R:
 If-Else Statement: Executes code based on conditions.
 if (x > 10) {
 print("x is greater than 10")
 } else {
 print("x is less than or equal to 10")
 }
 If-Else If-Else Statement: Handles multiple conditions.
 Logical Conditions: Use && (AND), || (OR) to combine conditions.

6. Loops in R
Loops are used to repeat actions:
 For Loop: Repeats a block of code for a specific number of times.
 for (i in 1:5) {
 print(i)
 }
 While Loop: Repeats as long as the condition is true.
 Break & Next:
o break stops the loop.
o next skips the current iteration.

7. Vectors in R
A vector is a basic data structure in R that holds multiple values of the same type.
 Creating Vectors: c(1, 2, 3, 4)
 Vector Operations: sum(), mean(), length()

PROGRAM:- 2
Installing R and R studio
Step 1: Installing R
1.1. Go to the R Project website
 Website: https://cran.r-project.org/
1.2. Choose Your Operating System
 Windows: Look for the "Download R for Windows" link, and click it.
o A new page will open with options for downloading the latest version of R.
Choose the "base" option.
 macOS: Click on "Download R for macOS", and select the appropriate version.
 Linux: Choose the distribution of Linux you are using and follow the instructions.
1.3. Install R
 Once you have downloaded the installer for your OS, double-click it to begin installation.
o Follow the on-screen instructions:
 Windows: Keep clicking “Next” and then “Install.”
 macOS: Drag the R icon to the Applications folder.
Step 2: Installing RStudio
2.1. Go to the RStudio Website
 Website: https://www.rstudio.com/
2.2. Download RStudio
 Click on “Download RStudio” and then select the RStudio Desktop option.
 Choose the free version and download the installer for your OS.
2.3. Install RStudio
 After the download is complete, run the installer:
o Windows: Double-click the .exe file and follow the prompts to install RStudio.
o macOS: Open the downloaded file and drag the RStudio icon to your
Applications folder.
o Linux: Follow the distribution-specific instructions to install RStudio.
Step 3: Opening RStudio
Once both R and RStudio are installed:
1. Open RStudio (on Windows, you can search for it in the Start Menu; on macOS, search
for it in Applications).
2. RStudio will automatically detect R and open an interface with multiple panes:
o Console: Where you type R code.
o Environment/History: Displays variables and previous commands.
o Files/Plots/Packages/Help: For managing files, viewing plots, and accessing help
documentation.
First Code in RStudio
 In the Console pane, type this code:

 Hit Enter, and you should see the output in the console:
PROGRAM 3
List the R packages
To list all installed R packages using R programming, you can use the installed.packages()
function. Here's the R code that will list the installed packages:

This will return a matrix with information about each installed package, including the package
name, version, and other details.
If you'd like to see just the names of the packages, you can extract them with this code:

This code will output a simple list of all the package names that are currently installed on your
system.

How to load a package


Once a package is installed, you need to load it into your R session with library():
PROGRAM 4
Introduction to basic functionality of R, variable, data types in R
1. R Variables
In R, variables are used to store data that can be referenced and manipulated later. A variable is
created by assigning a value to a name using the <- operator or the = operator.
# Create variables using <-
x <- 5
y <- 10

# Create variables using =


a = 20
b = 30

# Print values
print(x)
print(y)
print(a)
print(b)
Expected Output:

2. Data Types in R
R supports several basic data types. The most common are:
 Numeric: Numbers (both integers and decimals).
 Character: Strings of text.
 Logical: Boolean values (TRUE or FALSE).
 Complex: Complex numbers (not often used in basic operations).

num1 <- 3.14 # Numeric Data Type


num2 <- 42
text <- "Hello, R!" # Character Data Type
is_true <- TRUE # Logical Data Type
is_false <- FALSE
complex_num <- 2 + 3i # Complex Data Type
# Print data types and values
print(num1)
print(num2)
print(text)
print(is_true)
print(is_false)
print(complex_num)
# Check data types using class() function
print(class(num1))
print(class(text))
print(class(is_true))
print(class(complex_num))
Expected Output:
3. Operations on Variables and Data Types
R allows for mathematical operations on numeric variables, logical comparisons, and string
concatenation.
sum <- x + y # Arithmetic operations
product <- num1 * num2
is_equal <- (x == y) # Logical operations
greater_than <- (x > y)
greeting <- paste(text, "Welcome to the world of R!") # String concatenation
# Print results
print(sum)
print(product)
print(is_equal)
print(greater_than)
print(greeting)
Expected Output:

4. Using Vectors in R
A vector is a basic data structure in R that can hold multiple values. You can perform operations
on entire vectors at once.
nums <- c(1, 2, 3, 4, 5) # Create a numeric vector
sum_of_elements <- sum(nums) # Perform operations on the vector
mean_of_elements <- mean(nums)
# Print vector and results
print(nums)
print(sum_of_elements)
print(mean_of_elements)
Expected Output:

5. Data Frame in R
A data frame is a table-like structure where each column can have different data types.
# Create a data frame
df <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Age = c(25, 30, 35),
Height = c(5.5, 6.0, 5.8))
# Print the data frame
print(df)

# Access specific elements


print(df$Name) # Print the Name column
print(df[1, 2]) # Print the element in row 1, column 2
Expected Output:
PROGRAM 5
Implement R script to show the usage of various operators available in R language
# 1. Arithmetic Operators
a <- 10
b <- 5
sum_result <- a + b
diff_result <- a - b
prod_result <- a * b
div_result <- a / b
exp_result <- a ^ b
mod_result <- a %% b

# Print the results of arithmetic operations


cat("Arithmetic Operations:\n")
cat("a + b = ", sum_result, "\n")
cat("a - b = ", diff_result, "\n")
cat("a * b = ", prod_result, "\n")
cat("a / b = ", div_result, "\n")
cat("a ^ b = ", exp_result, "\n")
cat("a %% b = ", mod_result, "\n")
# 2. Relational Operators
x <- 20
y <- 30
equal_result <- x == y
not_equal_result <- x != y
greater_than_result <- x > y
less_than_result <- x < y
greater_than_equal_result <- x >= y
less_than_equal_result <- x <= y

# Print the results of relational operations


cat("\nRelational Operations:\n")
cat("x == y: ", equal_result, "\n")
cat("x != y: ", not_equal_result, "\n")
cat("x > y: ", greater_than_result, "\n")
cat("x < y: ", less_than_result, "\n")
cat("x >= y: ", greater_than_equal_result, "\n")
cat("x <= y: ", less_than_equal_result, "\n")

3. Logical Operators
p <- TRUE
q <- FALSE
and_result <- p & q
or_result <- p | q
not_result <- !p

# Print the results of logical operations


cat("\nLogical Operations:\n")
cat("p & q: ", and_result, "\n")
cat("p | q: ", or_result, "\n")
cat("!p: ", not_result, "\n")

4. Assignment Operators
x <- 10 # Simple assignment
y <- 20
z <- 30
x <- y <- z # Assign value of z to y, and then y to x
# Print assignment results
cat("\nAssignment Operations:\n")
cat("After chained assignment x = ", x, ", y = ", y, ", z = ", z, "\n")

5. Matrix Operations
matrix1 <- matrix(1:6, nrow=2, ncol=3)
matrix2 <- matrix(7:12, nrow=2, ncol=3)
# Matrix multiplication (cross product)
matrix_prod <- matrix1 %*% t(matrix2)

# Print matrix multiplication result


cat("\nMatrix Multiplication (matrix1 %*% t(matrix2)):\n")
print(matrix_prod)

EXPERIMENT 6
Implement R script to demonstrate all operations on vector and list data structure.
Vector
vector1 <- c(1, 2, 3, 4, 5) # 1. Create a vector
print(vector1)

print(vector1[3]) # 2. Access 3rd element

vector1[2] <- 10 # 3. Modify 2nd element


print(vector1)

vector2 <- c(5, 4, 3, 2, 1) # 4. Vector addition


sum_vector <- vector1 + vector2
print(sum_vector)

mean_value <- mean(vector1) # 5. Applying functions to vectors


sum_value <- sum(vector1)
print(paste("Mean of vector1:", mean_value))
print(paste("Sum of vector1:", sum_value))

logical_vector <- vector1 > 3 # 6. Vector logical operations


print("Logical comparison (elements > 3):")
print(logical_vector)

Lists
list1 <- list(name = "Alice", age = 25, scores = c(85, 90, 88)) # 1. Create a list
print(list1)

print(list1$name) # 2. Access 'name'

list1$age <- 26 # 3. Modify 'age'


print(list1)

list1$hobby <- "Reading" # 4. Add a new element


print(list1)

# 5. Nested lists
nested_list <- list(person = list(name = "Bob", age = 30), scores = c(70, 80, 90))
print(nested_list)
age_mean <- mean(list1$scores) # 6. Applying functions on list elements
print(paste("Mean of scores in the list:", age_mean))

PROGRAM 7
Implement R script to demonstrate all operations on array and matrix data structure.
Array
array1 <- array(1:12, dim = c(3, 4)) # 1. Create an array
print(array1)

print(array1[2, 3]) # 2. Access a specific element in the array


array1[2, 3] <- 20 # 3. Modify an element in the array
print(array1)

array2 <- array(13:24, dim = c(3, 4)) # 4. Array arithmetic (addition)


sum_array <- array1 + array2
print(sum_array)

# 5. Apply a function to an array (sum of all elements)


sum_array_elements <- sum(array1)
print(paste("Sum of all elements in array1:", sum_array_elements))

Matrices

matrix1 <- matrix(1:9, nrow = 3, byrow = TRUE) # Create a 3x3 matrix # 1. Create a matrix
print(matrix1)

print("Access element at position (2, 3):") # 2. Access a specific element in the


matrix
print(matrix1[2, 3])

matrix1[1, 2] <- 20 # 3. Modify an element in the matrix


print(matrix1)

# 4. Matrix multiplication
matrix2 <- matrix(1:9, nrow = 3, byrow = FALSE) # Another 3x3 matrix
product_matrix <- matrix1 %*% matrix2
print("Matrix multiplication (matrix1 * matrix2):")
print(product_matrix)

# 5. Apply a function to a matrix (mean of each column)


mean_matrix <- apply(matrix1, 2, mean)
print("Mean of each column in the matrix:")
print(mean_matrix)
Program 8
Operations on Data Frame and Factor Data Structures
Data Frame Operations -
data <- data.frame(
ID = 1:5,
Name = c("John", "Mary", "Alice", "Bob", "Tom"),
Age = c(23, 34, 25, 30, 29),
Gender = c("Male", "Female", "Female", "Male", "Male")
) # 1. Create a Data Frame
print("Data Frame:")
print(data)

subset_data <- subset(data, Age > 25) # 2. Subset a Data Frame (rows where Age
> 25)
print("Subset Data Frame (Age > 25):")
print(subset_data)

age_column <- data$Age # 3. Access a specific column


print("Age Column:")
print(age_column)

data$Salary <- c(50000, 60000, 55000, 65000, 62000) # 4. Add a new column to the
Data Frame
print("Data Frame with New Salary Column:")
print(data)
data$Name[1] <- "Jon" # 5. Modify a column (e.g., changing Name
of the first row)
print("Modified Data Frame (First Name Changed to 'Jon'):")
print(data)

mean_age <- mean(data$Age) # 6. Apply function to a column (e.g., calculating


the mean of Age)
print(paste("Mean Age:", mean_age))

Factor Operations
gender_factor <- factor(data$Gender) # 1. Create a Factor
print("Factor (Gender):")
print(gender_factor)

levels_gender <- levels(gender_factor) # 2. Levels of the Factor


print("Levels of Gender Factor:")
print(levels_gender)

gender_factor <- relevel(gender_factor, ref = "Female") # 3. Relevel a Factor


print("Releveled Gender Factor:")
print(gender_factor)

# 4. Add a New Level to the Factor


gender_factor <- factor(gender_factor, levels = c(levels(gender_factor), "Non-Binary"))
print("Gender Factor with New Level 'Non-Binary':")
print(gender_factor)

gender_count <- table(gender_factor) # 5. Table of Factor Counts


print("Count of Each Gender:")
print(gender_count)

PROGRAM9
Conditional and Decision-Making Statements in R
# 1. If-Else Statement
if (x > 10) {
x <- 15
print("x is greater than 10")
} else {
print("x is less than or equal to 10")}

# 2. If-Else If-Else Statement


y <- 75
if (y >= 90) {
print("Grade: A")
} else if (y >= 80) {
print("Grade: B")
} else if (y >= 70) {
print("Grade: C")
} else {
print("Grade: F")}

# 3. Nested If-Else Statement


age <- 20
citizen <- TRUE
if (age >= 18) {
if (citizen) {
print("Eligible to vote")
} else {
print("Not eligible to vote: Not a citizen")}
} else {
print("Not eligible to vote: Underage")}

# 4. Logical Operators in Conditions


a <- 5
b <- 10
if (a > 0 && b > 0) {
print("Both a and b are positive")
} else {
print("Either a or b is not positive")}

# 5. Switch Statement
choice <- 3
result <- switch(choice,
"1" = "You selected One",
"2" = "You selected Two",
"3" = "You selected Three",
"Invalid Choice")
print(result)

# 6. Ternary Operator (Using ifelse)


num <- -8
sign <- ifelse(num > 0, "Positive", ifelse(num < 0, "Negative", "Zero"))
print(paste("The number is:", sign))

# 7. Using ifelse for Vectorized Operations


scores <- c(85, 45, 90, 60, 70)
grades <- ifelse(scores >= 75, "Pass", "Fail")
print("Grades for Scores:")
print(grades)
PROGRAM 10
Implement Different Functions in R Programming
Functions in R are used to perform specific tasks by executing a set of statements. R provides
built-in functions as well as the ability to define user-defined functions. Functions improve code
reusability and modularity.
Types of functions in R:
1. Built-in Functions
2. User-defined Functions
3. Recursive Functions
4. Anonymous (Lambda) Functions
1. Built-in Functions
# Using built-in functions
x <- c(2, 4, 6, 8, 10)
print(sum(x)) # Sum of elements
print(mean(x)) # Mean of elements
print(sd(x)) # Standard deviation

2. User-defined Functions
# Function to calculate the square of a number
square <- function(num) {
return(num^2) }
print(square(5))

3. Recursive Function
# Factorial function using recursion
factorial_func <- function(n) {
if (n == 0) return(1)
else return(n * factorial_func(n - 1)) }
print(factorial_func(5))
4. Anonymous Function (Lambda Function)
# Applying an anonymous function
result <- (function(a, b) a + b)(10, 20)
print(result) # Output: 30

PROGRAM 11
Reading Different Types of Data Sets from Web or Disk and Writing to a
Specific Location, and Reading Excel Data in R
R provides functions for reading and writing different file formats such as:
 read.table() and read.csv() for text and CSV files.
 write.table() and write.csv() for writing data to disk.
 readxl package for reading Excel files.
# read airtravel.csv file from our current directory
read_data <- read.csv("airtravel.csv")
print(read_data)

# read airtravel.csv file from our directory


read_data <- read.csv("airtravel.csv")
cat("Total Columns: ", ncol(read_data))
cat("Total Rows:", nrow(read_data))

# read airtravel.csv file from our directory


read_data <- read.csv("airtravel.csv")
min_data <- min(read_data$1960) # 390
min_data <- max(read_data$1958) # 505

PROGRAM 12
Implement R Script to Create a Pie Chart, Bar Chart, Scatter Plot, and Histogram
Data visualization helps in understanding data patterns and trends. R provides several functions
to create different types of charts:
 Pie Chart (pie()) - Used to represent categorical data proportionally.
 Bar Chart (barplot()) - Represents categorical data with rectangular bars.
 Scatter Plot (plot()) - Displays relationships between two numerical variables.
 Histogram (hist()) - Represents the distribution of a numerical variable.
1. Pie Chart
# Create a vector of pies
x <- c(10,20,30,40)

# Create a vector of labels


mylabel <- c("Apples", "Bananas", "Cherries", "Dates")

# Display the pie chart with labels


pie(x, label = mylabel, main = "Fruits")

2. Bar Chart
# x-axis values
x <- c("A", "B", "C", "D")

# y-axis values
y <- c(2, 4, 6, 8)

barplot(y, names.arg = x)
3. Scatter Plot
x <- c(5,7,8,7,2,2,9,4,11,12,9,6)
y <- c(99,86,87,88,111,103,87,94,78,77,85,86)

plot(x, y, main="Observation of Cars", xlab="Car age", ylab="Car speed")

4. Histogram
temperatures <- c(67 ,72 ,74 ,62 ,76 ,66 ,65 ,59 ,61 ,69 )

# histogram of temperatures vector


result <- hist(temperatures,
main = "Histogram of Temperature",
xlab = "Temperature in degrees Fahrenheit"
)
print(result)

PROGRAM 13
To perform basic statistical operations (mean, median, mode, range,summary,
variance, standard deviation) using R.
1. Creating a Dataset
data <- c(12, 15, 18, 22, 25, 28, 30, 35, 40, 45, 50, 55, 60)
2. Calculating Mean
mean_val <- mean(data)
print(paste("Mean:", mean_val))

3. Calculating Median
median_val <- median(data)
print(paste("Median:", median_val))

4. Calculating Mode
get_mode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]}
mode_val <- get_mode(data)
print(paste("Mode:", mode_val))

5. Calculating Range
range_val <- range(data)
print(paste("Range:", range_val[1], "to", range_val[2]))

6. Generating Summary Statistics


summary_stats <- summary(data)
print("Summary:")
print(summary_stats)
7. Calculating Variance
variance_val <- var(data)
print(paste("Variance:", variance_val))

8. Calculating Standard Deviation


sd_val <- sd(data)
print(paste("Standard Deviation:", sd_val))

PROGRAM 14
Implementing Normal and Binomial Distributions in R
1. Normal Distribution:
o A continuous probability distribution that is symmetric around the mean.

o Defined by two parameters: mean (μ\mu) and standard deviation (σ\sigma).

o R functions:

 dnorm(x, mean, sd): Computes the density function.


 pnorm(x, mean, sd): Computes the cumulative probability.
 rnorm(n, mean, sd): Generates random numbers.
 qnorm(p, mean, sd): Finds quantiles.
2. Binomial Distribution:
o A discrete probability distribution representing the number of successes in nn
trials.
o Defined by two parameters: number of trials (nn) and probability of success (pp).

o R functions:

 dbinom(k, size, prob): Computes the probability mass function.


 pbinom(k, size, prob): Computes the cumulative probability.
 rbinom(n, size, prob): Generates random numbers.
 qbinom(p, size, prob): Finds quantiles.
1. Normal Distribution
Dnorm()
x <- seq(-10, 10, by = .1)
y <- dnorm(x, mean = 2.5, sd = 0.5)
png(file = "dnorm.png")
plot(x,y)
dev.off()

Pnorm()
x <- seq(-10,10,by = .2)
y <- pnorm(x, mean = 2.5, sd = 2)
png(file = "pnorm.png")
plot(x,y)
dev.off()
Qnorm()
x <- seq(0, 1, by = 0.02)
y <- qnorm(x, mean = 2, sd = 1)
png(file = "qnorm.png")
plot(x,y)
dev.off()

Rnorm()
y <- rnorm(50)
png(file = "rnorm.png")
hist(y, main = "Normal DIstribution")
dev.off()
2. Binomial Dbinom()
x <- seq(0,50,by = 1)
y <- dbinom(x,50,0.5)
png(file = "dbinom.png")
plot(x,y)
dev.off()

Pbinom()
x <- pbinom(26,51,0.5)
print(x)

Qbinom()
x <- qbinom(0.25,51,1/2)
print(x)

Rbinom()
x <- rbinom(8,150,.4)
print(x)

PROGRAM 15
Implement R Script to perform correlation, Linear and multiple regression.
# Load necessary libraries
install.packages("ggplot2") # For visualization (optional)
library(ggplot2)

# Sample data
set.seed(123)
data <- data.frame(
x1 = rnorm(100, mean = 50, sd = 10),
x2 = rnorm(100, mean = 30, sd = 5),
y = rnorm(100, mean = 100, sd = 15)
)

# 1. Correlation
cor_x1_y <- cor(data$x1, data$y)
cor_x2_y <- cor(data$x2, data$y)
cat("Correlation between x1 and y: ", cor_x1_y, "\n")
cat("Correlation between x2 and y: ", cor_x2_y, "\n")

# 2. Linear Regression (Simple Linear)


# Fit a linear regression model for y based on x1
linear_model_x1 <- lm(y ~ x1, data = data)
summary(linear_model_x1)

# 3. Multiple Regression
multiple_model <- lm(y ~ x1 + x2, data = data)
summary(multiple_model)

You might also like