R code is the backbone of data analysis in this programming language. Learning to write and execute it effectively is crucial for your success. This section covers the basics of R syntax , including variables, functions , and operations.
You'll also discover how to run your code in different environments, manage your workspace, and handle errors. These skills form the foundation for more advanced R programming techniques you'll encounter later in the course.
Basic R Syntax
Core Syntax Elements
Top images from around the web for Core Syntax Elements Syntax for a programming language View original
Is this image relevant?
UBISSS16 D3.1 - R Markdown | Seeds for Change - next View original
Is this image relevant?
Syntax for a programming language View original
Is this image relevant?
1 of 3
Top images from around the web for Core Syntax Elements Syntax for a programming language View original
Is this image relevant?
UBISSS16 D3.1 - R Markdown | Seeds for Change - next View original
Is this image relevant?
Syntax for a programming language View original
Is this image relevant?
1 of 3
Variable Types and Operations
Numeric variables store numbers (x <- 10.5
)
Character variables contain text strings, enclosed in quotes (name <- "John"
)
Logical variables hold TRUE or FALSE values (is_student <- TRUE
)
Vector creation uses c()
function to combine multiple values (numbers <- c(1, 2, 3, 4, 5)
)
Arithmetic operations include addition (+
), subtraction (-
), multiplication (*
), and division (/
)
Comparison operators evaluate conditions (==
, !=
, <
, >
, <=
, >=
)
Function Basics and Examples
Built-in functions provide pre-programmed operations (mean, sum, length)
User-defined functions allow custom operations creation
Function structure includes name, arguments, and body (my_function <- function(x, y) { x + y }
)
Function arguments can have default values (greet <- function(name = "User") { paste("Hello", name) }
)
Return values specify function output (return()
statement or last expression)
Executing R Code
Running Code in R Environment
Interactive execution occurs in R console, entering commands one at a time
Script files (.R extension) allow multiple lines of code to be run together
Source command executes entire script file (source("script_name.R")
)
RStudio provides integrated development environment for R, offering code editor and console in one interface
Code chunks in R Markdown documents combine code and documentation
Command History and Workspace Management
Command history stores previously executed commands, accessible using up arrow key
history()
function displays recent commands in console
Save workspace image preserves current R session variables and data (save.image()
)
Load previously saved workspace restores saved session (load()
)
Clear workspace removes all objects from current session (rm(list = ls())
)
Error Handling and Debugging
Error messages provide information about code issues, helping identify and fix problems
Warning messages alert potential issues without stopping code execution
Debugging tools assist in identifying errors (browser() function, RStudio debugger)
Common errors include syntax errors, object not found, and incorrect data types
Error tracing helps locate source of errors in complex code structures
Working Directory and File Management
Working directory sets default location for file operations
getwd()
function displays current working directory
setwd()
function changes working directory (setwd("/path/to/directory")
)
Relative file paths reference files relative to working directory (read.csv("data/myfile.csv")
)
Absolute file paths specify complete file location (read.csv("/home/user/documents/data/myfile.csv")
)