Spring 2023
Smith College
To understand how R sees objects under the hood so we can better use those structures when we get programming.
You may be familiar with the class()
function.
It tells us the class of an object in our environment.
These are important to understand when doing data analyses.
R will default to the most inclusive (most general) class.
[1] 1 1
[1] "1" "TRUE"
[1] 1 0
Character > Numeric > Logical
R actually sees objects from a number of angles, class being only one. Type is another.
NA
& is.na()
NA
is used in place of unknown or missing values (though they aren’t the same in reality!). With few exceptions, whenever you try to do something to a NA
the result will be NA
.
NULL
& is.null()
NULL
is used then there is no value–it does not exist. NULL
does not take the place of another value, it has a length of 0. Contrast this to when there is a value, but it is unknown or missing.
NaN
& is.nan()
0 / 0
.
R can also keep meta-data on objects.
The easiest of these to see is names.
You may have used named vectors before; this is how they work!
Attributes are used all over for lots of common uses.
robert yuki K'nar'st
"cat" "dog" "fish"
Named chr [1:3] "cat" "dog" "fish"
- attr(*, "names")= chr [1:3] "robert" "yuki" "K'nar'st"
You can add your own custom attributes too!
In fact, many of the packages in R rely on attributes.
We’ll get into that more later this semester.
$names
[1] "robert" "yuki" "K'nar'st"
$age
[1] 4 6 NaN
[1] Lavender Pink orange Light purple Blue
[6] Green
16 Levels: black Black blue Blue Brown Crimson Green grey ... wine red
Lists are kinda like super-vectors (JSON-like).
They can contain anything in their elements. You could have:
You can ask for data in a specific position in a vector by giving it the number of that position.
For example, if vector <- c(1, 3, 5, 7, 9).
Getting the content of lists requires special syntax!
Each list element is accessed using double square brackets [[ ]]
```{r}
test_list = list("num_vec" = c(1, 2, 3, 4, 5),
"let_vec" = c("a", "b", "c", "c"),
"df" = head(mtcars))
test_list
```
$num_vec
[1] 1 2 3 4 5
$let_vec
[1] "a" "b" "c" "c"
$df
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
IF
example_vec_1 = c(1, 2, 3)
example_vec_2 = c(“a”, “b”, “c”)
AND
example_list = list(example_vec_1, example_vec_2)
THEN
example_list[[1]] == example_vec_1 == c(1, 2, 3)
example_list[[2]][3] == example_vec_2[3] == “c”
You already do!
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
$names
[1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
[11] "carb"
$row.names
[1] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710"
[4] "Hornet 4 Drive" "Hornet Sportabout" "Valiant"
[7] "Duster 360" "Merc 240D" "Merc 230"
[10] "Merc 280" "Merc 280C" "Merc 450SE"
[13] "Merc 450SL" "Merc 450SLC" "Cadillac Fleetwood"
[16] "Lincoln Continental" "Chrysler Imperial" "Fiat 128"
[19] "Honda Civic" "Toyota Corolla" "Toyota Corona"
[22] "Dodge Challenger" "AMC Javelin" "Camaro Z28"
[25] "Pontiac Firebird" "Fiat X1-9" "Porsche 914-2"
[28] "Lotus Europa" "Ford Pantera L" "Ferrari Dino"
[31] "Maserati Bora" "Volvo 142E"
$class
[1] "data.frame"
Sometimes, things don’t really need to be a list.
You may run into this situation when creating functions later on.
It’s possible to “flatten” lists into a vector if they are simple enough.
Lists let you store any arbitrary structure of data.
This makes them very powerful.
We will learn to harness this power in time.
Functions
SDS 270: Advanced Programming for Data Science