R Programming - Detailed Exam Notes
1. Overview of R
- R is a programming language and software environment mainly used for
- It is open-source and free.
- Highly popular in academia, research, and data science industries.
- Supports data visualization using packages like ggplot2.
Main Points:
- R is free and open-source.
- Excellent for statistics and data analysis.
- Powerful data visualization capabilities.
2. R Data Types and Objects
Data Types:
- Numeric: Decimal values (Example: 3.5)
- Integer: Whole numbers (Example: 4L)
- Character: Text strings (Example: "Data")
- Logical: TRUE or FALSE values
- Complex: Numbers with imaginary parts (Example: 3 + 2i)
Objects:
- Vector: Collection of same-type elements.
- List: Can hold mixed data types.
- Matrix: Two-dimensional arrangement of elements.
- Data Frame: Table-like structure with rows and columns.
- Factor: Used for categorical variables like "Male", "Female".
Main Points:
- Data types: Numeric, Integer, Character, Logical, Complex.
- Objects: Vector, List, Matrix, Data Frame, Factor.
3. Reading and Writing Data
- Read CSV File: read.csv("filename.csv")
- Read Text File: read.table("filename.txt")
- Write CSV File: write.csv(data, "output.csv")
Main Points:
- Use read.csv() to load data.
- Use write.csv() to save data.
4. Subsetting R Objects
- Vectors: Access elements with indices (Example: x[2])
- Matrices: Access with row and column (Example: m[1,2])
- Data Frames: Access by row and column name (Example: df[1, "Name"])
Main Points:
- Subset data using indices.
- For data frames, use column names.
5. Essentials of the R Language
- Installing R: Download from CRAN.
- Running R: Use R Console or RStudio.
- Packages: Install with install.packages("package"), load with library(pack
Main Points:
- Install R and RStudio.
- Packages are essential to add new functions.
6. Calculations in R
- Supports basic arithmetic: +, -, *, /
- Handles complex math easily.
- Example: 7 * 4 = 28
Main Points:
- R can perform all types of calculations easily.
7. Complex Numbers in R
- Defined as: x <- 2 + 3i
- Use Re(x) to get the real part.
- Use Im(x) to get the imaginary part.
Main Points:
- R can process complex numbers.
- Real and imaginary parts can be separated.
8. Rounding in R
- round(3.456, 2) gives 3.46
- ceiling(2.3) gives 3 (next higher integer)
- floor(4.7) gives 4 (next lower integer)
Main Points:
- Rounding helps in simplifying decimal values.
9. Arithmetic, Modulo, and Integer Quotients
- Modulo: 7 %% 3 gives remainder 1
- Integer Division: 7 %/% 3 gives quotient 2
Main Points:
- Use %% for remainder.
- Use %/% for integer division.
10. Variable Names and Assignment
- Variables are assigned using <- or =
- Example: x <- 10
- Variable names must start with a letter and cannot have spaces.
Main Points:
- Use <- for assigning values.
- Variable names should be meaningful.
11. Operators in R
+ Addition
- Subtraction
* Multiplication
/ Division
^ Power
%% Modulo
%/% Integer Division
Main Points:
- Operators are used to perform arithmetic operations.
12. Integers in R
- Created by adding L: x <- 5L
- class(x) will show "integer"
- Useful when working with whole numbers only.
Main Points:
- L is used to specify integers.
13. Factors in R
- Created using factor() function.
- Example: gender <- factor(c("Male", "Female"))
- Useful for grouping data.
Main Points:
- Factors are used for categorical data.
14. Logical Operations
== Equal
!= Not equal
> Greater than
< Less than
>= Greater or equal
<= Less or equal
& AND
| OR
! NOT
Example: (5 > 3) & (4 < 6) gives TRUE
Main Points:
- Logical operations help in making comparisons.
- Useful in filtering data and decision-making.
Final Summary
- R is easy to learn and powerful for data analysis.
- Master basic data types, calculations, and logical operations.
- Practice reading and writing data.
- Understand how to subset and manipulate data.
- Use R packages to extend functionality.