Australian National University · FACULTY OF STATISTICS

BIOL2001 Chap.1 Values, Data Types and Data Structures in R

- one subject, every graph, every model, every mark
8 Chapters3-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 1 of 15 · BIOL2001

Values, Data Types and Data Structures in R

The first week of R

Part I opens with how an R session works: you type a command at the prompt, R reads it, evaluates it and prints the result.

The chapter covers the arithmetic operators, including integer division and modulo, the logical operators that return TRUE or FALSE, and the assignment arrow that stores a value under a name.

Types and structures

Every value has a type: integer, double, logical or character, checked with typeof(). Missing and impossible values have their own markers, NA, Inf, NaN and NULL.

Values are then organised into structures.

R has no scalars, so the simplest structure is the vector, and the chapter places vectors, matrices, lists and data frames on a map defined by two questions: do the elements share one type, and are there one or two dimensions?

Why it matters later

Every later chapter, from indexing to regression output, assumes you can tell a vector from a list and a double from a character string at a glance.

In this chapter

What this chapter covers

  • 01

    The read-evaluate-print loop and RStudio

  • 02

    Arithmetic operators including %/% and %%

  • 03

    Logical operators and TRUE or FALSE results

  • 04

    Assignment with the arrow operator

  • 05

    Integer, double, logical and character types

  • 06

    NA, Inf, NaN and NULL

  • 07

    Atomic vectors and why R has no scalars

  • 08

    Matrices, lists and data frames compared

Worked example · free

Placing four objects on the structure map

Q [4 marks]. For each object, name the R structure and justify it: counts of frogs at three ponds; the numbers 1 to 12 arranged in three rows; a record holding a site name, a rain flag and a count; and a field sheet with one row per pond and columns for name, area and depth. The practice weighting is assigned by AskSia and is not an official assessment.
The four structures used in Part I placed by two questions: are all elements the same type, and does the structure have one dimension or two?The four structures used in Part I placed by two questions: are all elements the same type, and does the structure have one dimension or two? Vectors and matrices hold one type; lists and data frames can mix types.one dimensiontwo dimensionssame type(homogeneous)mixed types(heterogeneous)vectorc(4, 7, 9)matrixmatrix(1:12, nrow = 3)listlist(2, TRUE, "Amy")data framecolumns of equal length
The four structures used in Part I placed by two questions: are all elements the same type, and does the structure have one dimension or two? Vectors and matrices hold one type; lists and data frames can mix types.
  • 1The pond counts share one type and run in one dimension, so they form an atomic vector, for example c(4, 0, 7).
  • 1The numbers 1 to 12 in rows and columns share one type in two dimensions: a matrix, matrix(1:12, nrow = 3).
  • 1The record mixes text, a logical and a number in one dimension, so it must be a list.
  • 1The field sheet has two dimensions with a different type in each column: a data frame.
Vector, matrix, list and data frame. The two questions that decide every case are whether all elements share one type and whether the structure has one dimension or two.
Sia tip — Check the type question first: as soon as one element is text and another is a number, a vector or matrix is ruled out.
Glossary

Key terms

Atomic Vector
A one-dimensional R structure whose elements all share a single type.
Data Type
The kind of value stored, such as integer, double, logical or character.
Modulo Operator
The operator %% that returns the remainder after dividing one number by another.
Missing Value
The marker NA, which holds the place of an unknown value in any type of vector.
Integrated Development Environment
Software such as RStudio that combines a console, a script editor, plots and help in one window.
FAQ

Values, Data Types and Data Structures in R FAQ

What is the difference between a data type and a data structure in R?

A data type describes a single piece of data, such as integer, double, logical or character. A data structure describes how pieces are organised, such as a vector, matrix, list or data frame. A vector of integers has vector as its structure and integer as its type.

Why does typeof(10) return double rather than integer?

R stores a plain number such as 10 as a floating-point double by default. To store it as an integer you add the L suffix, writing 10L, and typeof(10L) then returns integer. The difference rarely changes a calculation but explains the output you see.

What is the difference between NA, NaN and NULL?

NA marks a value that is missing but exists, NaN is the result of a calculation with no sense such as 0 divided by 0, and NULL is an empty object with no value at all. NA has type logical on its own, NaN is a double and NULL has its own type.

Is R case sensitive?

Yes. Assigning a value to x and then another to X creates two separate variables, and calling a function with the wrong capitalisation fails. Consistent lower-case names avoid a common source of errors in timed coding tasks.

Study strategy

Assessment move

Open RStudio beside this chapter and run every operator in the table, then predict the output of typeof() for ten values of your own before checking. Build one example of each structure from a dataset you know, such as counts from a field trip, and say aloud why it is that structure. Finish by creating a vector containing NA and checking it with is.na().

A good self-test is to explain, without running anything, why typeof(NA) is logical while typeof(NaN) is double, and why a vector of length one is how R stores what other languages call a scalar.

Working through Values, Data Types and Data Structures in R in BIOL2001? Sia is AskSia’s AI Statistics tutor — ask any BIOL2001 Values, Data Types and Data Structures in R question and get a clear, step-by-step explanation grounded in how BIOL2001 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 46 of your Australian National University subjects - and 1,000+ Bibles across every Australian university.
Sia - your BIOL2001 tutor, unlimited, worked the way the exam marks it
The full 3-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works