You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

1.4 Writing and executing R code

3 min readaugust 9, 2024

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 , including variables, , 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
Top images from around the web for Core Syntax Elements
  • R syntax follows a clear and consistent structure enabling efficient data manipulation and analysis
  • begin with
    #
    symbol, allowing developers to annotate code for clarity and documentation
  • Variables store data values, assigned using
    <-
    or
    =
    operators (
    x <- 5
    or
    x = 5
    )
  • Functions perform specific tasks, called using parentheses and arguments (
    function_name(argument1, argument2)
    )

Variable Types and Operations

  • store numbers (
    x <- 10.5
    )
  • contain text strings, enclosed in quotes (
    name <- "John"
    )
  • hold TRUE or FALSE values (
    is_student <- TRUE
    )
  • creation uses
    c()
    function to combine multiple values (
    numbers <- c(1, 2, 3, 4, 5)
    )
  • include addition (
    +
    ), subtraction (
    -
    ), multiplication (
    *
    ), and division (
    /
    )
  • evaluate conditions (
    ==
    ,
    !=
    ,
    <
    ,
    >
    ,
    <=
    ,
    >=
    )

Function Basics and Examples

  • provide pre-programmed operations (mean, sum, length)
  • allow custom operations creation
  • includes name, arguments, and body (
    my_function <- function(x, y) { x + y }
    )
  • can have default values (
    greet <- function(name = "User") { paste("Hello", name) }
    )
  • specify function output (
    return()
    statement or last expression)

Executing R Code

Running Code in R Environment

  • occurs in R console, entering commands one at a time
  • (.R extension) allow multiple lines of code to be run together
  • executes entire script file (
    source("script_name.R")
    )
  • provides integrated development environment for R, offering code editor and console in one interface
  • in R Markdown documents combine code and documentation

Command History and Workspace Management

  • stores previously executed commands, accessible using up arrow key
  • history()
    function displays recent commands in console
  • preserves current R session variables and data (
    save.image()
    )
  • Load previously saved workspace restores saved session (
    load()
    )
  • removes all objects from current session (
    rm(list = ls())
    )

Error Handling and Debugging

  • provide information about code issues, helping identify and fix problems
  • alert potential issues without stopping code execution
  • assist in identifying errors (browser() function, RStudio debugger)
  • include syntax errors, object not found, and incorrect data types
  • helps locate source of errors in complex code structures

Working Directory and File Management

  • sets default location for file operations
  • getwd()
    function displays current working directory
  • setwd()
    function changes working directory (
    setwd("/path/to/directory")
    )
  • reference files relative to working directory (
    read.csv("data/myfile.csv")
    )
  • specify complete file location (
    read.csv("/home/user/documents/data/myfile.csv")
    )
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary