2  R Basics

2.1 Assignment Operator

You assign an R object to a value using the assignment operator <- or the equal sign. <- is read as “gets”.

x <- y
d <- read.csv('mydata.csv')
x = y

2.2 Object Types

Everything in R is an object. Some primitive types of objects in R are below.

Type Meaning
integer whole numbers
logical values of TRUE or FALSE
double floating point non-whole numbers
character character strings
function code defining a function

In the table below, objects of different shapes are described. rows and cols refers to vectors of integers or logicals, or if the elements of the object are named, character strings.

Type Example Values Retrieved By
scalar x <- 3 x
vector y <- c(1, 2, 5) y[2] (2), y[2:3] (2, 5), y[-1] (2, 5), y[c(TRUE,FALSE,TRUE)] (1, 5)
named vector y <- c(a=1, b=2, d=5) y[2] (2), y['b'] (2), y[c('a','b')] (1, 2)
matrix y <- cbind(1:3, 4:5) y[rows,cols], y[rows,] (all cols), y[,cols] (all rows)
list x <- list(a='cat', b=c(1,3,7)) x$a (‘cat’), x[[1]] (‘cat’), x[['a']] (‘cat’)

Named vectors provide an extremely quick table lookup and recoding capability.

list objects are arbitrary trees and can have elements nested to any level. You can have lists of lists or lists of data frames/tables.

Vectors can be of many different types when a class is added to them. Two of the most common are Dates and factors. Character strings are handled very efficiently in R so there is not always a need to store categorical variables as factors. But there is one reason: to order levels, i.e., distinct variable values, so that tabular and graphical output will list values in a more logical order than alphabetic. A factor variable has a levels attribute added to it to accomplish this. An example is x <- factor(x, 1:3, c('cat', 'dog', 'fox')) where the second argument 1:3 is the vector of possible numeric values x currently takes on (in order) and the three character strings are the corresponding levels. Internally factors are coded as integers, but they print as character strings.

Rectangular data objects, i.e., when the number of rows is the same for every column (variable), can be represented by matrices, data.frames, and data.tables. In a matrix, every value is of the same type. A data.frame or a data.table is an R list that can have mixtures of numeric, character, factor, dates, and other object types. A data.table is also a data.frame but the converse isn’t true. data.tables are handled by the R data.table package and don’t have row names but can be indexed, are much faster to process, and have a host of methods implemented for aggregation and other operations. data.frames are handled by base R.

Data frames are best managed by converting them to data tables and using the data.table package. When data.table is not used there are three indispensable functions for operating on data frames:

  • with for analyzing variables within a data frame without constantly prefixing variable names with dataframename$
  • transform for adding or changing variables within a data frame
  • Hmisc upData function for doing the same as transform but also allowing metadata to be added to the data, e.g., variable labels and units (to be discussed later)

Here are some examples of with and transform.

# Better than mean(mydata$systolic.bp - mydata$diastolic.bp) :
with(mydata, mean(systolic.bp - diastolic.bp))
# Better than mydata$pulse.pressure <- mydata$systolic.bp - mydata$diastolic.bp:
mydata <- transform(mydata,
                    pulse.pressure = systolic.bp - diastolic.bp,
                    bmi            = wt / ht ^ 2)
# Perform several operations on the same data frame
with(mydata, {
               x3 <- x1 / sqrt(x2)
               ols(y ~ x3)
             }  )

2.3 Subscripting

Examples of subscripting are given above. Subscripting via placement of [] after an object name is used for subsetting, and occasionally for using some elements more than once:

x <- c('cat', 'dog', 'fox')
[1] "dog" "fox"
x[c(1, 1, 3, 3, 2)]
[1] "cat" "cat" "fox" "fox" "dog"

Subscripting a variable or a data frame/table by a vector of TRUE/FALSE values is a very powerful feature of R. This is used to obtain elements satisfying one or more conditions:

x <- c(1, 2, 3, 2, 1, 4, 7)
y <- c(1, 8, 2, 3, 8, 9, 2)
x[y > 7]
[1] 2 1 4

The last line of code can be read as “values of x such that y > 7”.

2.4 Branching and If/Then

2.4.1 Decisions Based on One Scalar Value

Common approaches to this problem are if and switch.

type <- 'semiparametric'
f <- switch(type,
            parametric     = ols(y ~ x),
            semiparametric = orm(y ~ x),
            nonparametric  = rcorr(x, y, type='spearman'),
            { z <- y / x
              c(median=median(z), gmean=exp(mean(log(z)))) } )
# The last 2 lines are executed for any type other than the 3 listed
f <- if(type == 'parametric')    ols(y ~ x)
    if(type == 'semiparametric') orm(y ~ x)
    if(type == 'nonparametric')  rcorr(x, y, type='spearman')
  else {
    z <- y / z
    c(median=median(z), gmean=exp(mean(log(z)))

What is inside if( ) must be a single scalar element that is evaluated to whether it’s TRUE or FALSE.

2.4.2 Series of Separate Decisions Over a Vector of Values

The ifelse or data.table::fifelse functions are most often used for this, but data.table::fcase is a little better. Here’s an example.

x <- c('cat', 'dog', 'giraffe', 'elephant')
type <- ifelse(x %in% c('cat', 'dog'), 'domestic', 'wild')
[1] "domestic" "domestic" "wild"     "wild"    
fcase(x %in% c('cat', 'dog'), 'domestic', default='wild')
[1] "domestic" "domestic" "wild"     "wild"    

2.4.3 if Trick

Sometimes when constructing variable-length vectors and other objects, elements are to be included in the newly constructed object only when certain conditions apply. When a condition does not apply, no element is to be inserted. We can capitalize on the fact that the result of if(...) is NULL when ... is not TRUE, and concatenating NULL results in ignoring it. Here are two examples. In the first the resulting vector will have length 2, 3, or 4 depending on sex and height. In the second example the new vector will have the appropriate element names preserved.

y <- 23; z <- 46; sex <- 'female'; height <- 71; u <- pi; w <- 7
c(y, z, if(sex == 'male') u, if(height > 70) w)
[1] 23 46  7
c(x1=3, if(sex == 'male') c(x2=4), if(height > 70) c(x3=height))
x1 x3 
 3 71 
# reduce clutter in case of variable name conflicts:
rm(y, z, sex, height, u, w)

2.5 Functions

There are so many functions in R that it may be better to use the stackoverflow.com Q&A to find the ones you need (as of 2022-05-26 there are 450,000 R questions there). Here are just a few of the multitude of handy R functions. The first functions listed below return the R missing value NA if any element is missing. You can specify na.rm=TRUE to remove NAs from consideration first, so they will not cause the result to be NA. Most functions get their arguments (inputs) in () after the function name. Some functions like %in% are binary operators whose two arguments are given on the left and right of %in%.

  • mean, median, quantile, var, sd: Compute statistical summaries on one vector
  • min, max: Minimum or maximum of values in a vector or of multiple variables, resulting in one number
  • pmin, pmax: Parallel minimum and maximum for vectors, resulting in a vector. Example: pmin(x, 3) returns a vector of the same length as x. Each element is the minimum of the original value or 3.
  • range: Returns a vector of length two with the minimum and maxmum
  • unique: Return vector of distinct values, in same order as original values
  • union, intersect, setdiff, setequal: Set operations on two vectors (see below)
  • a %in% b, a %nin% b: Set membership functions that determine whether each element in a is in b (for %in%) or is not in b (for %nin%, which is in the Hmisc package)

Set operators are amazingly helpful. Here are some examples.

unique(x)       # vector of distinct values of x, including NA if occurred
sort(unique(x)) # distinct values in ascending order
setdiff(unique(x), NA)  # distinct values excluding NA if it occurred
duplicated(x)   # returns TRUE for elements that are duplicated by
                # values occurring EARLIER in the list
union(x, y)     # find all distinct values in the union of x & y
intersect(x, y) # find all distinct values in both x & y
setdiff(x, y)   # find all distinct x that are not in y
setequal(x, y)  # returns TRUE or FALSE depending on whether the distinct
                # values of x and y are identical, ignoring how they
                # are ordered

Find a list of subject ids that are found in baseline but not in follow-up datasets:

idn <- setdiff(baseline$id, followup$id)

Avoid repetition: Don’t say if(animal == 'cat' | animal == 'dog') ....; use %in% instead:

if(animal %in% c('cat', 'dog')) ...
# or if(animal %in% .q(cat, dog)) ...

Likewise don’t say if(animal != 'cat' & animal != 'dog') but use if(animal %nin% c('cat', 'dog')) ...

To get documentation on a function type the following in the R console: ?functionname or ?packagename::functionname.

Even new R users can benefit from writing functions to reduce repetitive coding. A function has arguments and these can have default values for when the argument is not specified by the user when the function is called. Here are some examples. One line functions do not need to have their bodies enclosed in {}.

cuberoot <- function(x) x ^ (1/3)
[1] 2
g <- function(x, power=2) {
  u <- abs(x - 0.5)
  u / (1. + u ^ power)
g(3, power=2)
[1] 0.3448276
[1] 0.3448276

Write a function make mean() drop missing values without our telling it.

mn <- function(x) mean(x, na.rm=TRUE)

Function to be used throughout the report to round fractional values by a default amount (here round to 0.001):

rnd <- function(x) round(x, 3)
# edit the 3 the change rounding anywhere in the report

A simple function to save coding when you need to recode multiple variables from 0/1 to no/yes:

yn <- function(x) factor(x, 0:1, c('no', 'yes'))

2.6 Resources for Learning R