Introduction to R Programming - Summary
1. Data Types / Objects in R
- Numeric: Decimal numbers (e.g., 3.14)
- Integer: Whole numbers (e.g., 10L)
- Character: Text data (e.g., 'Hello')
- Logical: TRUE or FALSE
- Complex: Numbers with imaginary parts (e.g., 4 + 2i)
- Raw: Byte-level data
Use class(), typeof(), is.numeric() etc. to check types.
2. Creating and Manipulating Factors
- Factors represent categorical data.
- Created with factor(): factor(c('low', 'medium', 'high'))
- Can be ordered: factor(..., ordered=TRUE)
- Useful in statistical modeling and grouping.
3. Creating and Manipulating Vectors
- Vectors are 1D homogeneous data structures.
- Created with c(), seq(), rep().
- Access via [index], modify via assignment.
- Supports vectorized operations.
4. Creating and Manipulating Matrices
- Matrices are 2D homogeneous data structures.
- Created with matrix(), rbind(), cbind().
- Access via [row, column].
- Supports transpose, multiplication, row/column sums.
5. Creating and Manipulating Lists
- Lists hold heterogeneous elements.
- Created with list().
- Access using $, [[ ]], or [ ].
- Can be nested; useful for complex data.
6. Creating and Manipulating Data Frames
- Data frames are 2D tabular structures with columns as vectors.
- Columns can be different data types.
- Created with data.frame().
- Access with df[row, col], df$colname.
- Use read.csv(), write.csv() to import/export.
7. Subsetting Matrices and Data Frames
- Extract specific rows, columns, or elements.
- Use [row, col], negative indexing, logical indexing.
- For data frames, use df[df$Age > 20, ] or subset() function.