Data types

Author

Jeffrey R. Stevens

Published

February 3, 2023

Creating objects

First, let’s create some objects:

aa <- 3; bb <- 3L; cc <- "3"; dd <- "TRUE"; ee <- TRUE; ff <- "NA"; gg <- NA

aa <- 3
bb <- 3L
cc <- "3"
dd <- "TRUE"
ee <- TRUE
ff <- "NA"
gg <- NA

Checking data types

Guess what data type each object is then check it.

typeof(aa)
[1] "double"
typeof(bb)
[1] "integer"
typeof(cc)
[1] "character"
typeof(dd)
[1] "character"
typeof(ee)
[1] "logical"
typeof(ff)
[1] "character"
typeof(gg)
[1] "logical"

How do we test if aa is an integer?

is.integer(aa)
[1] FALSE

What will is.logical(dd) return?

is.logical(dd)
[1] FALSE

How do we test if ff and gg are NA?

is.na(ff)
[1] FALSE
is.na(gg)
[1] TRUE

Checking if objects are the same

Are aa and bb the same? How do we test this?

aa
[1] 3
bb
[1] 3
aa == bb
[1] TRUE

What about aa and cc?

aa
[1] 3
cc
[1] "3"
aa == cc
[1] TRUE

A safer comparison tool is identical(). Test if aa and bb are identical. Then try aa and cc.

identical(aa, bb)
[1] FALSE
identical(aa, cc)
[1] FALSE

Now see if aa is identical to 3 and if bb is identical to 3L.

identical(aa, 3)
[1] TRUE
identical(bb, 3L)
[1] TRUE