R Course Notes
R Course Notes
1. ASSIGNMENT OPERATORS IN R
You can use either of these to assign a value to a variable. Here are examples of both:
Important Tip: Both = and <- work similarly, but <- is often preferred by R programmers
as a convention.
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
a=1
b=2
c=3
1. Addition (+):
a*b #1*2=2
5. Modulus (%%): Returns the remainder when dividing the first number by the
second (useful for finding out if a number is divisible by another).
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
Example Comparisons:
a = 15
b = 10
c = 20
2. EQUALITY OPERATORS
III. Not Equal (!=): Checks if two values are not equal.
3. DATA TYPES IN R
a) Numeric Data
A sequence of numbers:
print(num)
b) Character Data
Character Strings:
print(chr)
c) Logical Data
lg <- c(TRUE, FALSE, TRUE) # Logical values without quotes are treated as logical
type
print(lg)
d) Integer Data
print(int)
e) Complex Data
comp <- 3 + 2i # A complex number with a real part (3) and imaginary part (2i)
print(comp)
g) Conversions
4. DATA STRUCTURES IN R
1. Vector:
o Type: One-dimensional, same data type.
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.
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.
To work with data in R, you first need to import it. Here are some options for importing
data into R Studio:
1.CSV Files:
2. Excel Files:
3.Other Formats:
You can use packages like haven for SPSS, Stata, or SAS files.
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.
Example:
After installing a package, you need to load it using the library() function so you can use
its functions.
Tip: You need to load the library every time you start a new R session.
Note: read_csv() is provided by the readr package, which makes reading CSV files
efficient.
Purpose: dplyr is a very popular package for data manipulation. It provides easy-to-use
functions for filtering, selecting, arranging, and summarizing data.
4. SPELLING MISTAKES:
install.packages("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()..
install.packages("package_name")
Load the Library (each time you start a new session):
library(package_name)
Memorization Tips:
Rhyme to Remember: