Advanced Topics in R Programming
1. Factors and Tables
Factors in R are used to represent categorical data. They store both the values and the
corresponding levels.
Example:
gender <- factor(c("Male", "Female", "Female", "Male", "Female"))
table(gender) shows the frequency count of each category.
Tables are used to generate frequency distributions using the table() function.
Example:
table(gender, age_group)
2. Accessing Input and Output
R allows input from users via readline(), and data from files using read.csv() or read.table().
Example:
name <- readline("Enter your name: ")
data <- read.csv("data.csv")
Output can be printed using print() or cat(), and written to files using write.csv() or
write.table().
Example:
write.csv(data, "output.csv")
3. Graphs in R
R supports extensive visualization tools.
Basic plotting functions:
plot(x, y), hist(), barplot(), boxplot()
Advanced plotting using ggplot2:
ggplot(data = mpg, aes(x = displ, y = hwy)) + geom_point()
These tools are essential for data analysis and pattern recognition.
4. R Apply Family
The apply family of functions are vectorized alternatives to loops.
apply(): Applies functions to rows or columns of matrices.
lapply(): Applies functions to list elements, returns list.
sapply(): Like lapply but returns vector/matrix when possible.
tapply(): Applies functions over subsets defined by a factor.
mapply(): Multivariate apply to multiple arguments.
Examples:
apply(matrix(1:9, nrow=3), 1, sum)
lapply(list(a=1:5, b=6:10), mean)
sapply(list(a=1:5, b=6:10), mean)
tapply(c(1,2,3,4,5,6), factor(c("A","A","B","B","C","C")), sum)
mapply(sum, 1:5, 6:10)