library(dslabs)
data(murders)
str to examine the structure of the
Murders object.str(murders)
## 'data.frame': 51 obs. of 5 variables:
## $ state : chr "Alabama" "Alaska" "Arizona" "Arkansas" ...
## $ abb : chr "AL" "AK" "AZ" "AR" ...
## $ region : Factor w/ 4 levels "Northeast","South",..: 2 4 4 2 4 4 1 2 2 2 ...
## $ population: num 4779736 710231 6392017 2915918 37253956 ...
## $ total : num 135 19 232 93 1257 ...
A. The 51 states.
B. The murder rates for all 50 states and DC.
C. The state name, the abbreviation of the state name, the state’s region, and the state’s population and total number of murders for 2010.
D. str shows no relevant information.
C. The state name, the abbreviation of the state name, the state’s region, and the state’s population and total number of murders for 2010.
colname <- names(murders)
colname
## [1] "state" "abb" "region" "population" "total"
$ to extract the state abbreviations
and assign them to the object a. What is the class of this
object?# Extract state abbreviations
a <- murders$abb
# Check the class
class(a)
## [1] "character"
b. Use the identical
function to determine if a and b are the
same.# Extract state abbreviations using square brackets
b <- murders[["abb"]]
# Check if 'a' and 'b' are identical
identical(a, b)
## [1] TRUE
region column stores a factor. You can
corroborate this by typing: class(murders$region)With one line of code, use the function levels and
length to determine the number of regions defined by this
data-set.
# Get the levels of the 'region' column
region_levels <- levels(murders$region)
# Get the number of regions
num_regions <- length(region_levels)
num_regions
## [1] 4
temp.temp <- c(35, 88, 42, 84, 81, 30)
temp
## [1] 35 88 42 84 81 30
city.city <- c("Beijing", "Lagos", "Paris", "Rio de Janeiro", "San Juan", "Toronto")
city
## [1] "Beijing" "Lagos" "Paris" "Rio de Janeiro"
## [5] "San Juan" "Toronto"
names function and the objects defined in the
previous exercises to associate the temperature data with its
corresponding citynames(temp) <- city
names(temp)
## [1] "Beijing" "Lagos" "Paris" "Rio de Janeiro"
## [5] "San Juan" "Toronto"
[ and : operators to access the
temperature of the frst three cities on the list.temp[1:3]
## Beijing Lagos Paris
## 35 88 42
[ operator to access the temperature of Paris
and San Juan.temp[c("Paris", "San Juan")]
## Paris San Juan
## 42 81
: operator to create a sequence of numbers 12,
13, 14,…, 73.seq(12, 73)
## [1] 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
## [26] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
## [51] 62 63 64 65 66 67 68 69 70 71 72 73
seq(1, 99, by = 2)
## [1] 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
## [26] 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
seq and
length.increment_vector <- seq(6, 55, by = 4/7)
length(increment_vector)
## [1] 86
a <- seq(1, 10, 0.5) ?a <- seq(1, 10, 0.5) is
“numeric”.a <- seq(1, 10) ?a <- seq(1, 10) is
“numeric”.class(a<-1) is numeric, not integer.
R defaults to numeric and to force an integer, you need to
add the letter L. Confirm that the class of 1L
is integer.1L is “integer.” Adding “L” to a number in
R forces it to be an integer.x <- c("1", "3", "5")
and coerce it to get integers.x <- c("1", "3", "5")
int_x <- as.integer(x)
class(int_x)
## [1] "integer"