End Interactive Session 2A
<- c("teddy", "khora", "waffle", "banjo")
dogs
typeof(dogs)
[1] "character"
class(dogs)
[1] "character"
Data types & indexing
eds221-day2-comp
r-py-data-types
There is a hierarchy of classes. The broadest of all in a vector wins (if there are characters, then character will be the class of the entire vector).
Use []
with the position or name to access elements of a vector.
Or we can specify a range of values within a vector using [:]
. The first element in R vectors is assigned element = 1. This is an important distinction. In Python, the first element is assigned 0 (zero-index).
(…we did some of this in EDS 212 too!)
[,1] [,2]
[1,] 0.8 0.4
[2,] 1.2 0.9
[1] "double"
[1] "matrix" "array"
What happens if we try to combine multiple data types into a matrix?
Index using [row, column]
.
whale_travel <- matrix(data = c(31.8, 1348, 46.9, 1587), nrow = 2, ncol = 2, byrow = TRUE)
# Take a look
whale_travel
[,1] [,2]
[1,] 31.8 1348
[2,] 46.9 1587
[1] 1348
[1] 46.9
If you leave any element blank (keeping the comma), it will return all values from the other element. For example, to get everything in row 2:
Or, to access everything in column 1:
What happens if I only give a matrix one element? That’s the position in the matrix as if populated by column. Check out a few:
[[1]]
[1] "blue"
[[2]]
[1] 1 2 3
[[3]]
[1] "a cat" "a dog"
[[4]]
[1] 5
Important: a single [] returns a list. [[]] returns the item STORED in the list.
tacos <- list(topping = c("onion", "cilantro", "guacamole"), filling = c("beans", "meat", "veggie"), price = c(6.75, 8.25, 9.50))
# The whole thing
tacos
$topping
[1] "onion" "cilantro" "guacamole"
$filling
[1] "beans" "meat" "veggie"
$price
[1] 6.75 8.25 9.50
[1] "beans" "meat" "veggie"
[1] "beans" "meat" "veggie"
A data frame is a list containing vectors of the same length, where each column is a variable stored in a vector. Let’s make one:
fruit <- data.frame(type = c("apple", "banana", "peach"),
mass = c(130, 195, 150))
# Look at it
fruit
type mass
1 apple 130
2 banana 195
3 peach 150
[1] "data.frame"
Use [row#, col#], or name the column (then element number).
array([1, 2, 8])
<class 'numpy.ndarray'>
A list is mutable - you can change it directly!
A tuple is immutable - you’ll get yelled at if you try to change it!
A more involved list (note: you can also use list() to create lists in python).
[['cat', 'dog', 'penguin'], 2, 'a burrito', [1, 2, 5]]
# Access an element from the list waffle:
waffle[0] # Default just returns that piece (not as a list)
['cat', 'dog', 'penguin']
We can reassign pieces of a list:
['screech', 'squeal', 'bark']
[2, 6, 10]
In R:
state sales
1 CA 38000
2 NV 4670
3 OR 2750
In Python:
home_sales = {'state': ["CA", "NV", "OR"], 'sales': [38000, 4670, 2750]}
home_sales = pd.DataFrame(home_sales)
home_sales
state sales
0 CA 38000
1 NV 4670
2 OR 2750
End Interactive Session 2A