← R EnglishChapter 01 of 13

Introduction to R

## Learning Objectives - Understand R history and applications - Set up R development environment - Write and run your first R program - Understand R syntax basics ## What is R? R is a free, open-source programming language and environment for statistical computing and graphics. ```r # Your first R program print("Hello, World!") ``` ## Why Use R? ### Key Applications - **Statistical Analysis** - Comprehensive statistical tools - **Data Visualization** - Publication-quality graphics - **Machine Learning** - Predictive modeling packages - **Data Science** - Tidyverse ecosystem - **Academic Research** - Widely used in academia ### R vs Other Languages | Feature | R | Python | |---------|---|--------| | Primary Focus | Statistics | General purpose | | Visualization | Excellent | Good | | Statistics Packages | Extensive | Growing | | Learning Curve | Moderate | Moderate | | Community | Academic | Industry | ## Installing R ### Download R Get R from your local CRAN mirror at ### Install RStudio RStudio is the recommended IDE for R: ```r # Check your R version R.version ``` ### Verify Installation ```r # In R console or RStudio R.version R.version.string ``` ## Your First Program ### Hello World ```r # Print to console print("Hello, World!") # Or simply type the string "Hello, World!" ``` ### Comments ```r # Single-line comment # This is a single line comment x <- 5 # Inline comment # Multi-line comments need # on each line # Line 1 of comment # Line 2 of comment ``` ## R Syntax Basics ### Assignment ```r # Preferred assignment operator x <- 10 y <- "hello" # = also works but <- is idiomatic z = 20 # Check values x # Prints x print(x) # Explicit print ``` ### Variable Naming ```r # Valid names my_var <- 5 myVar <- 5 MY_VAR <- 5 myvar2 <- 5 # Invalid names # 2myvar <- 5 # Can't start with number # my-var <- 5 # No hyphens # my var <- 5 # No spaces ``` ## R Console Basics ### Basic Operations ```r # Arithmetic 2 + 3 # Addition: 5 5 - 2 # Subtraction: 3 4 * 3 # Multiplication: 12 10 / 2 # Division: 5 2 ^ 3 # Power: 8 5 %% 2 # Modulus: 1 ``` ### Built-in Functions ```r # Math functions sqrt(16) # Square root: 4 abs(-10) # Absolute value: 10 log(10) # Natural log log10(10) # Log base 10 exp(2) # e^2 round(3.14159, 2) # Round: 3.14 ``` ### Getting Help ```r # Help for a function ?mean help("mean") # Search help ??regression # Examples for a function example(mean) ``` ## RStudio IDE ### Interface Components - **Source Editor** - Write and edit scripts - **Console** - Run R code interactively - **Environment** - View variables and data - **Files/Plots/Packages** - Navigate files and view output ### Creating a Script ```r # File -> New File -> R Script # Write your code # Run with Ctrl+Enter (Windows/Linux) or Cmd+Enter (Mac) # Example script hello <- function(name) { paste("Hello,", name, "!") } hello("World") ``` ## R Packages ### Installing Packages ```r # From CRAN install.packages("ggplot2") # Multiple packages install.packages(c("dplyr", "tidyr", "readr")) ``` ### Loading Packages ```r # Load a package library(ggplot2) # Require with error message require(dplyr) ``` ## R Versions ```r # Check version details R.version R.version.string # Major version R.version$major R.version$minor ``` | Version | Year | Key Features | |---------|------|--------------| | R 3.x | 2013-2020 | Tidyverse, RStudio | | R 4.x | 2021+ | Improved performance | | R 4.3 | 2023 | Native pipe improvements | ## R Projects ### Creating a Project In RStudio: File -> New Project ### Working Directory ```r # Get current directory getwd() # Set directory setwd("/path/to/directory") # Relative paths work within project read.csv("data/myfile.csv") ``` ## Summary - R is designed for statistics and data analysis - Use `<-` for assignment (idiomatic R) - RStudio is the recommended IDE - Use `?function` for help - Install packages with `install.packages()` - Load packages with `library(package)`

Comments

Comments powered by Giscus

To enable comments, add your Giscus embed code here.

Learn more about Giscus →