Lecture Notes Of Class 1
Introduction to R Programming
Objective:
By the end of this lecture,
students will:
- Understand
the basics of R programming.
- Learn
about the R environment and its applications in data science.
- Get
familiar with installing and setting up R and RStudio.
- Perform
basic operations such as arithmetic calculations and variable assignments.
- Learn
about fundamental data structures like vectors.
1.
Introduction to R and RStudio
What is
R?
R is a powerful programming
language primarily used for statistical computing and data analysis. It is
widely used in academia, research, and industry for data visualization, machine
learning, and data manipulation.
Features
of R:
- Open-source
and free to use.
- Highly
extensible with numerous libraries and packages.
- Strong
support for data visualization (e.g., ggplot2).
- Used
in statistical modeling and machine learning.
What is
RStudio?
RStudio is an integrated
development environment (IDE) for R. It provides a user-friendly interface for
writing, running, and debugging R code.
Key Features of RStudio:
- A
console for executing R commands.
- A
script editor for writing and saving code.
- A
workspace to manage variables and data.
- A
plotting window for visualizing data.
2. Installing R and RStudio
Step 1:
Installing R
1. Visit
CRAN (Comprehensive R Archive Network).
2. Choose
your operating system (Windows, macOS, or Linux).
3. Download
and install the latest version of R.
Step 2:
Installing RStudio
1. Visit
RStudio’s official website.
2. Download
the free version of RStudio Desktop.
3. Install
RStudio on your computer.
After installation, open RStudio
and ensure that it is correctly linked to R.
3. R
Environment: Console, Script Editor, and Workspace
Console:
- The
console is where you can type and execute R commands interactively.
- Example:
print("Hello, R!")
Output:
[1] "Hello, R!"
Script
Editor:
- The
script editor allows you to write and save R scripts (.R files).
- Writing
code in scripts helps in reusability and documentation.
Workspace
and Environment:
- The Workspace
contains all variables and objects created during the session.
- The Environment
Pane in RStudio shows these objects.
- Use
the ls() function to list all objects:
ls()
4. Basic
Operations in R
Arithmetic
Operations:
R can perform basic mathematical
operations:
|
Operation |
Symbol |
Example |
Output |
|
Addition |
+ |
5 + 3 |
8 |
|
Subtraction |
- |
5 - 3 |
2 |
|
Multiplication |
* |
5 * 3 |
15 |
|
Division |
/ |
10 / 2 |
5 |
|
Exponentiation |
^ |
2^3 |
8 |
Example:
a <- 10
b <- 5
sum <-
a + b
print(sum) # Output: 15
Variables
in R:
- Variables
store data values in R.
- Example:
x <- 100
y <- "R
Programming"
z <- TRUE
Data
Types in R:
R has several basic data types:
|
Data
Type |
Example |
|
Numeric |
10, 3.14 |
|
Character |
"Hello, R" |
|
Logical |
TRUE, FALSE |
Check the data type using the class()
function:
num_var <- 50
char_var <-
"Data Science"
log_var <- TRUE
print(class(num_var)) # Output: "numeric"
print(class(char_var))
# Output: "character"
print(class(log_var)) # Output: "logical"
5.
Introduction to Vectors and Data Structures
What is a
Vector?
A vector is a basic data
structure in R that holds elements of the same type.
Creating
Vectors:
Use the c() function to create
vectors:
numbers <- c(1, 2, 3, 4, 5)
names <-
c("Alice", "Bob", "Charlie")
logical_vals
<- c(TRUE, FALSE, TRUE)
Vector
Operations:
- Access
elements:
numbers[2] # Output: 2
- Modify
elements:
numbers[1] <- 10
- Vector
arithmetic:
numbers + 5 # Adds 5 to each element in the vector
6.
Practical Exercises
Exercise
1: Install R and RStudio
- Follow
the steps discussed in the installation section.
- Open
RStudio and verify that R is working correctly by typing print("Hello,
R!") in the console.
Exercise
2: Perform Basic Arithmetic Operations
1. Open
RStudio and type the following commands in the console:
10 + 5
20 - 8
4 * 6
9 / 3
2^4
2. Observe
the outputs.
Exercise
3: Create and Manipulate Vectors
1. Create a
vector named marks containing student scores:
marks <- c(85, 90, 78, 88, 92)
2. Access
the third score:
marks[3]
3. Increase
each score by 5:
marks <- marks + 5
4. Print the
updated marks vector.
Conclusion
In this class, we covered:
✅ What R and RStudio are and their applications.
✅ How to install and set up R and RStudio.
✅ The basic components of the R environment:
Console, Script Editor, and Workspace.
✅ Performing arithmetic operations and working with
variables.
✅ Introduction to vectors, a fundamental data
structure in R.
In the next class, we will dive
deeper into Data Structures in R, including matrices, lists, and data
frames.
Assignment
(For Practice at Home)
1. Install R
and RStudio on your computer.
2. Write an
R script to:
o
Declare three variables: a numeric, a character,
and a logical variable.
o
Print their values and data types.
3. Create a
vector of 5 numbers and perform basic arithmetic operations on it.
Solution 1: Install R and RStudio
Follow these steps:
1. Download R
from CRAN (Comprehensive R Archive Network).
2. Install R
by following the instructions for your operating system.
3. Download RStudio
from RStudio’s official website.
4. Install
RStudio and open it to verify the installation.
5. In RStudio, test the installation by typing the following command in the console:
print("R
and RStudio are installed successfully!")
If you see the output, the
installation is successful.
Solution
2: R Script to Declare Variables and Print Their Values and Data Types
Save the following script as assignment1.R
and run it in RStudio.
# Declaring a numeric variable
num_var <- 100
#
Declaring a character variable
char_var <- "R Programming"
#
Declaring a logical variable
log_var <- TRUE
#
Printing values
print(num_var) # Output: 100
print(char_var) # Output: "R Programming"
print(log_var) # Output: TRUE
#
Printing data types
print(class(num_var)) # Output: "numeric"
print(class(char_var)) # Output: "character"
print(class(log_var)) # Output: "logical"
Solution
3: Create a Vector and Perform Arithmetic Operations
Save the following script as vector_operations.R and run it.
#
Creating a vector with 5 numbers
numbers <- c(10, 20, 30, 40, 50)
#
Printing the original vector
print("Original
vector:")
print(numbers)
# Performing arithmetic operations
# Adding
5 to each element
print("After
adding 5:")
print(numbers + 5)
#
Multiplying each element by 2
print("After
multiplying by 2:")
print(numbers * 2)
# Finding
the square of each element
print("Squares
of elements:")
print(numbers^2)
#
Accessing the third element
print("Third
element of the vector:")
print(numbers[3])
Expected
Output
[1] "Original vector:"
[1] 10 20
30 40 50
[1] "After
adding 5:"
[1] 15 25
35 45 55
[1] "After
multiplying by 2:"
[1] 20 40
60 80 100
[1] "Squares
of elements:"
[1] 100 400
900 1600 2500
[1] "Third
element of the vector:"
[1] 30
Final
Notes
✅ Run
these scripts in RStudio to see the outputs.
✅ Experiment with different values to deepen
your understanding.
✅ Save your scripts in .R files for future
reference.

No comments:
Post a Comment