x, and "Andrew" to name)3.14)2L)"hello")TRUE, FALSE)Difference:
Basic types are the fundamental building blocks for data storage in R.
Classes (e.g. S3) add structure and behavior to objects, enabling custom methods and more complex data structures.
x <- 3.14x this value, what does typeof(x) return? (this function tells us the basic type of an object)y <- 5L (L suffix makes it an integer)typeof(y)?name <- "Alice"paste() or paste0():nchar() (number of characters), toupper() (uppercase), and tolower() (lowercase) operate on character objectsTRUE or FALSE values.
flag <- TRUEclass(species)?typeof(species)? (And why?)scores <- c(88, 92, 79, 85)c(), seq(), or rep()arr <- array(1:8, dim = c(2, 2, 2))class(df) and typeof(df) return for df as defined on the last slide?typeof() for basic typetypeof() function tells you how R stores an object internally.
[1] "double"
[1] "integer"
class() for higher level classclass() function tells you the object’s class, which determines how R treats it in generic functions.
[1] "numeric"
[1] "factor"
[1] "data.frame"
[1] "list"
str() to inspect objectsstr() function gives a compact summary of an object’s structure.
'data.frame': 2 obs. of 1 variable:
$ name: chr "Alice" "Bob"
Factor w/ 2 levels "Cat","Dog": 2 1
num 3.14
[1] "1" "a" "TRUE"
[1] 1 2
as.numeric()as.character()as.logical()as.factor()[1] 123
[1] TRUE FALSE TRUE
NA and may give a warningc() to combine values into a vector:seq() to generate sequences:rep() to repeat values:NA.NA can appear in any type of object: numeric, character, logical, etc.
is.na():NA usually return NA unless you explicitly remove or handle (to be discussed next week)