BudiBadu Logo
Samplebadu
R

Learn R by Examples

R 4.x

R is a programming language and free software environment for statistical computing and graphics. It is widely used by statisticians and data miners for developing statistical software and data analysis.

These examples cover the core data structures of R, such as vectors, matrices, and data frames, as well as its powerful plotting capabilities and functional programming features.

Vectors

Understanding the fundamental data structure with this sample code demonstrating vector creation using c() function, homogeneous type enforcement, element-wise arithmetic operations, 1-based indexing system, and named vector elements.

Data Frames

Working with tabular data structures through this code example showing data frame construction with heterogeneous columns, dollar sign and bracket notation for column access, logical subsetting for row filtering, and dynamic column addition.

Lists

Managing heterogeneous collections with this sample code demonstrating list creation with mixed element types, single versus double bracket indexing behavior, dollar sign access for named elements, and dynamic list modification.

Factors

Representing categorical data efficiently with this code example showing factor creation from character vectors, level definition and ordering, internal integer storage with character labels, and type conversion behavior.

Functions

Defining reusable code blocks with this sample code demonstrating function creation with default parameters, variable argument lists using ellipsis, return value behavior, and anonymous function syntax for inline operations.

Apply Family

Applying functions over data structures without explicit loops through this code example showing apply for matrices, lapply returning lists, sapply simplifying results, and tapply for grouped operations on vectors.

Conditionals

Implementing decision logic with this sample code demonstrating if-else statements for scalar conditions, vectorized ifelse function for element-wise conditionals, switch statement for multiple options, and logical operator usage.

Plotting

Creating data visualizations with this code example demonstrating base R plot function for scatter and line plots, layered graphics using lines and legend functions, customization parameters for colors and styles, and histogram generation.

Packages

Extending R functionality with this sample code demonstrating package installation from CRAN, library loading for namespace access, ggplot2 for grammar of graphics visualization, dplyr for data manipulation with pipe operator, and double colon notation.