2  R Basics

3 R as a Calculator

1 + 1
[1] 2
3 * (4 + 5)
[1] 27

4 Objects & Assignment

x <- 10
y <- 3.5
x + y
[1] 13.5

5 object naming rules

  • R variable names can contain letters, numbers, periods, and underscores. However, they cannot start with a number or underscore. R is case-sensitive, so age, Age, and AGE would be considered different variables.
  • R variable names should be descriptive and meaningful. Avoid using reserved words or function names as variable names.
  • A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for R variables are:
  • A variable name must start with a letter and can be a combination of letters, digits, period(.) and underscore(_). If it starts with period(.), it cannot be followed by a digit.
  • A variable name cannot start with a number or underscore (_) Variable names are case-sensitive (age, Age and AGE are three different variables) Reserved words cannot be used as variables (TRUE, FALSE, NULL, if…)
  • Variable names should not contain spaces. Use underscore (_) or period (.) to separate words in a variable name.
  • Variable names should be meaningful and descriptive. Avoid using single-letter variable names except for temporary variables in loops or functions.

6 Basic Operations in R

R supports various basic operations, including: * Arithmetic Operations: Addition (+), subtraction (-), multiplication (*), division (/), and exponentiation (^). Example:

a <- 10
b <- 5
sum <- a + b
diff <- a - b
prod <- a * b
quot <- a / b
exp <- a ^ b
sum; diff; prod; quot; exp
[1] 15
[1] 5
[1] 50
[1] 2
[1] 1e+05
  • Comparison Operations: Equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). Example:
x <- 10
y <- 5
eq <- x == y
neq <- x != y
gt <- x > y
lt <- x < y
gte <- x >= y
lte <- x <= y
eq; neq; gt; lt; gte; lte
[1] FALSE
[1] TRUE
[1] TRUE
[1] FALSE
[1] TRUE
[1] FALSE
  • Logical Operations: AND (&), OR (|), and NOT (!). Example:
p <- TRUE
q <- FALSE
and <- p & q
or <- p | q
not <- !p
and; or; not
[1] FALSE
[1] TRUE
[1] FALSE

7 Comments in R

Comments in R are created using the # symbol. Anything following the # on the same line is considered a comment and is ignored by R during execution. Example:

# This is a comment
x <- 10  # Assigning value to x
y <- 5   # Assigning value to y
sum <- x + y  # Calculating the sum of x and y
sum  # Output the sum
[1] 15

8 Getting Help in R

R provides several ways to get help and documentation for functions and packages: * ?function_name: Displays the documentation for a specific function. Example:

?mean
  • help(function_name): Another way to access the documentation for a function. Example:
help(mean)
  • help.search("keyword"): Searches for help topics related to a specific keyword. Example:
help.search("regression")
  • example(function_name): Shows examples of how to use a specific function. Example:
example(mean)

mean> x <- c(0:10, 50)

mean> xm <- mean(x)

mean> c(xm, mean(x, trim = 0.10))
[1] 8.75 5.50
  • vignette("package_name"): Opens the vignette (detailed documentation) for a specific package. Example:
vignette("dplyr")
starting httpd help server ... done
  • ??keyword: Searches for help topics related to a specific keyword (similar to help .search). Example:
??regression

9 Installing and Loading Packages in R

R has a vast ecosystem of packages that extend its functionality. To use a package, you need to install it first and then load it into your R session. * Installing a Package: Use the install.packages("package_name") function to install a package from CRAN. Example:

install.packages("ggplot2")
  • Loading a Package: Use the library(package_name) function to load an installed package into your R session. Example:
library(ggplot2)

# Now you can use functions from the ggplot2 package

9.0.1 Saving and Loading Workspaces in R

You can save your R workspace (all objects in memory) to a file and load it later * Saving Workspace: Use the save.image("file_name.RData") function to save the entire workspace to a file. Example:

save.image("my_workspace.RData")
  • Loading Workspace: Use the load("file_name.RData") function to load a saved workspace from a file. Example:
load("my_workspace.RData")

10 Working Directory

getwd()
[1] "/home/runner/work/r4sas/r4sas"
# setwd("/path/you/want") # avoid in reproducible code; prefer here::here() for projects

11 Vectors (Atomic)

nums <- c(1, 2, 3, 4)
chars <- c("a", "b", "c")
logical <- c(TRUE, FALSE, TRUE)
typeof(nums); typeof(chars); typeof(logical)
[1] "double"
[1] "character"
[1] "logical"

12 Exercises

  1. Create an object z that stores (5^2 + 7)/3.
  2. Use ?seq and create a sequence from 0 to 1 by 0.1.
  3. Inspect typeof() for a few objects you create.