LECTURE 1 NOTES
1. ASSIGNMENT OPERATORS IN R
Assignment operators are used to assign values to variables.
In R, there are two common assignment operators:
o = (equals)
o <- (left arrow)
You can use either of these to assign a value to a variable. Here are examples of both:
a=1 # Assigns value 1 to variable a
a <- 2 # Assigns value 2 to variable a using another assignment symbol
Important Tip: Both = and <- work similarly, but <- is often preferred by R programmers
as a convention.
a = "lecture" # Assigns the string "lecture" to a
a <- "z" # Assigns the string "z" to a
Memory Tip: You can think of <- as an arrow that is pointing to where the value goes.
Imagine shooting the value into the variable.
2. ARITHMETIC OPERATORS IN R
Arithmetic operators allow you to perform basic math operations.
Suppose we have three variables:
a=1
b=2
c=3
1. Addition (+):
Adds two numbers.
a+b #1+2=3
2. Subtraction (-): Subtracts the second number from the first.
a - b # 1 - 2 = -1
3. Multiplication (*): Multiplies two numbers.
a*b #1*2=2
4. Division (/): Divides the first number by the second.
a / b # 1 / 2 = 0.5
5. Modulus (%%): Returns the remainder when dividing the first number by the
second (useful for finding out if a number is divisible by another).
a %% b # 1 %% 2 = 1 (since 1 divided by 2 leaves a remainder of 1)
Memory Tip: Think of % as representing "pieces left," which helps you remember it
gives the remainder.
6. Exponentiation (^): Raises the first number to the power of the second number.
a^b #1^2=1
Memory Tip: The ^ symbol can be imagined as a tiny "house roof," symbolizing raising
up to a power.
LECTURE 2 NOTES
1. LOGICAL OPERATIONS (TRUE/FALSE COMPARISONS)
In R, you can perform comparisons to get True or False as the result.
Example Comparisons:
a = 15
b = 10
c = 20
Less than (<):
a < b # Is 15 less than 10? Answer: FALSE
Greater than (>):
a > b # Is 15 greater than 10? Answer: TRUE
Less than or equal to (<=):
a <= b # Is 15 less than or equal to 10? Answer: FALSE
Greater than (>) (with negative sign example):
a > -b # Is 15 greater than -10? Answer: TRUE
2. EQUALITY OPERATORS
I. Assignment (=): Assigns a value.
a = b # Assigns the value of b (10) to a.
II. Equality Check (==): Checks if two values are equal.
a == b # Is a equal to b? Answer: TRUE or FALSE (depends on current value of a)
III. Not Equal (!=): Checks if two values are not equal.
a != b # Is a not equal to b? Answer: FALSE if a == b, otherwise TRUE
3. DATA TYPES IN R
R supports different types of data, and it's important to recognize these:
a) Numeric Data
A sequence of numbers:
num <- c(1, 2, 3, 4, 5, 6, 7, 8) # `c()` creates a collection of numbers
print(num)
class(num) # Will return "numeric" since these are numbers
b) Character Data
Character Strings:
chr <- c("welcome to R studio")
print(chr)
class(chr) # This will return "character"
c) Logical Data
Logical Values: Represented by TRUE or FALSE.
lg <- c(TRUE, FALSE, TRUE) # Logical values without quotes are treated as logical
type
print(lg)
class(lg) # Will return "logical"
Tip: If you use quotes around "True" or "False", they will be treated as characters, not
logical values.
d) Integer Data
Integers: Numbers with an "L" suffix to explicitly declare them as integers.
int <- c(1L, 2L, 3L, 4L, 5L)
print(int)
class(int) # Will return "integer"
e) Complex Data
comp <- 3 + 2i # A complex number with a real part (3) and imaginary part (2i)
print(comp)
class(comp) # Will return "complex"
f) Factor Data Types
fac <- c(a, b, c) # Creating a collection of a, b, c
fac <- [Link](fac) # Convert `fac` to a factor (categorical data)
class(fac) # Will return "factor"
levels(fac) # Displays the unique levels in the factor
g) Conversions
Convert between data types, like changing integers to numeric:
int <- [Link](int) # Converts `int` to numeric data type
class(int) # Now will return "numeric"
4. DATA STRUCTURES IN R
1. Vector:
o Type: One-dimensional, same data type.
o Example: A list of gene expressions.
o Creation: c(1, 2, 3).
2. Matrix:
o Type: Two-dimensional, same data type.
o Example: Gene expression table.
o Creation: matrix(data, nrow, ncol).
3. Data Frame:
o Type: Two-dimensional, mixed data types (like a table).
o Example: Sample metadata.
4. List:
o Type: One-dimensional, mixed data types.
o Example: Experimental data with multiple datasets.
o Creation: list(a, b, "text", TRUE).
5. Factor:
o Type: One-dimensional, used for categorical data.
o Example: Treatment groups or experimental conditions.
Summary and Tips for Memory:
Logical Values: Use TRUE or FALSE without quotes for logical operations.
Integer Data: Add an "L" to make numbers explicit integers.
Factors: Think of factors like categories or labels; they help with grouping data.
5. GUIDE FOR DATA IMPORT, PACKAGE INSTALLATION, AND
LIBRARY USE IN R STUDIO
1. IMPORTING DATA IN R STUDIO
To work with data in R, you first need to import it. Here are some options for importing
data into R Studio:
Common Ways to Import Data:
[Link] Files:
Use the [Link]() or readr::read_csv() function to import CSV files.
data <- [Link]("path/to/your/[Link]") # Base R function
data <- readr::read_csv("[Link]") # Using `readr` package
2. Excel Files:
Use the readxl package for Excel files.
[Link]("readxl") # First install the readxl package
library(readxl) # Load the readxl library
data <- read_excel("path/to/your/[Link]") # Import an Excel file
[Link] Formats:
You can use packages like haven for SPSS, Stata, or SAS files.
[Link] Import Tool:
In R Studio, you can also use the GUI (Graphical User Interface) by clicking
“Import Dataset” in the Environment tab. This is a user-friendly way to import
data without coding.
2. INSTALLING PACKAGES IN R STUDIO
Packages are collections of R functions, data, and code to simplify certain tasks. To use
functions provided by a package, you need to install it first.
How to Install a Package:
Use the [Link]("package_name") function.
Example:
[Link]("readr") # Install the `readr` package for reading data
[Link]("dplyr") # Install the `dplyr` package for data manipulation
Tip: You only need to install a package once on your system.
3. LOADING (ATTACHING) LIBRARIES IN R STUDIO
After installing a package, you need to load it using the library() function so you can use
its functions.
How to Load a Package:
library(readr) # Loads the readr package
library(dplyr) # Loads the dplyr package
Tip: You need to load the library every time you start a new R session.
4. Installing and Loading Specific Packages & Functions
Install and Load readr:
[Link]("readr") # Install the readr package
library(readr) # Load the readr package into your session
Purpose: readr is used for reading data files like CSV, and it's faster and more flexible
than base R’s [Link]() function.
Import a CSV File ([Link]):
heart_data <- read_csv("[Link]") # Reads the "[Link]" file
Note: read_csv() is provided by the readr package, which makes reading CSV files
efficient.
Install and Load dplyr:
[Link]("dplyr") # Install the dplyr package
library(dplyr) # Load the dplyr package into your session
Purpose: dplyr is a very popular package for data manipulation. It provides easy-to-use
functions for filtering, selecting, arranging, and summarizing data.
Common Errors and How to Fix Them
4. SPELLING MISTAKES:
In your example, there are two mistakes. It should be [Link]() and
library(), not [Link]() or libary().
Correct Code:
[Link]("readr")
library(readr)
Package Not Found: If you forget to install a package, R will return an error.
Always make sure to install before using library()..
5. SUMMARY STEPS FOR INSTALLING AND USING A PACKAGE :
Install the Package (only needed once):
[Link]("package_name")
Load the Library (each time you start a new session):
library(package_name)
Use Functions from the Package:
# For example, reading data from a CSV
data <- read_csv("[Link]")
Memorization Tips:
Rhyme to Remember:
“Install to Set, Library to Get.”
Think of installing like setting up a tool in your workspace.
Think of loading the library like taking the tool out to use.