Data types

Jeff Stevens

2023-02-03

Review

Mental model of literate programming

Data types

Data types

Formats of individual elements

  • Double

  • Integer

  • Character

  • Logical

Check data types with typeof()

Mental model of data types

Numeric data

Doubles

Floating-point numbers with decimals

# assign value 7.2 to object a
(a <- 7.2) # remember, wrapping in parentheses prints to console
[1] 7.2
[1] "double"

Numeric data

Integers

Numbers without decimals

(b <- 7)
[1] 7
[1] "double"

Doubles can have 0 as decimal

Numeric data

Integers

Numbers without decimals (specified with L)

(c <- 7L)
[1] 7
[1] "integer"

Character data

Must be surrounded by ""

(d <- "Hello, world")
[1] "Hello, world"
(e <- "7")
[1] "7"
[1] "character"

Let’s explore!

  • Type b, c, and e into the console separately. What do you see?
  • Add b + c.
  • Add b + e.

Logical

Tests whether conditional statement is TRUE or FALSE (notice always all upper case)

Logical operators: >, >=, <, <=, ==, !=, %in%

a
[1] 7.2
a > 5
[1] TRUE
d
[1] "Hello, world"
(mytest <- d == "Good-bye, world")
[1] FALSE
typeof(mytest)
[1] "logical"

Logical

The logical operator for equals is ==

Note

We use

  • == for logical equals
  • <- for assigning objects
  • = for assigning function argument values to argument names

Logical

%in% operator: “is contained in”

(subjects <- c("01", "02", "03", "04", "05"))
[1] "01" "02" "03" "04" "05"
"03" %in% subjects
[1] TRUE
"06" %in% subjects
[1] FALSE

Test “is NOT contained in” with ! before test string

!"06" %in% subjects
[1] TRUE

Augmented data types

Core data types with special attributes

  • Factors

  • Dates

Factors

Augmented integers with ‘levels’

(i <- factor("married", levels = c("single", "married", "widowed")))
[1] married
Levels: single married widowed
[1] "integer"

Note

Use class() to view augmented data type

[1] "factor"

Dates

Augmented numerics based on number of days since 1970-01-01

(j <- as.Date("1970-01-01"))
[1] "1970-01-01"
[1] "double"
[1] "Date"

Note

Make sure to wrap dates in ""

Dates

You can do math on dates

(k <- as.Date("2023-02-03"))
[1] "2023-02-03"
k-j
Time difference of 19391 days

Check data types

  • Check in RStudio
[1] FALSE
[1] TRUE

Converting between data types (coercion)

Use as.<type>() functions:

as.logical(), as.numeric(), as.character()

e
[1] "7"
[1] "character"
(l <- as.numeric(e))
[1] 7
[1] "double"

Converting between data types (coercion)

(m <- "TRUE")
[1] "TRUE"
[1] "character"
(n <- as.logical(m))
[1] TRUE
[1] "logical"

Converting between data types (coercion)

Factors to numerics is tricky

(o <- factor(0, levels = c("1", "0")))
[1] 0
Levels: 1 0
[1] 2

First coerce to character

[1] "0"
[1] 0

Special values

NA represents missing values

  • Each data type has its own type of NA

  • Check with is.na()

NaN means “not a number” (undefined)

  • 0 / 0 = NaN

Inf and -Inf represent infinity and negative infinity

  • 1 / 0 = Inf

  • -1 / 0 = -Inf

Mental model of data types

Let’s code!

Data types coding [Rmd]