Load DSLABS Library & Call the Murders Data :-

library(dslabs) 
data(murders)
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 ...

Which of the following best describes the variables represented in this data frame?

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.

The best description of the variables represented in this data frame is:

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.

What are the column names used by the data frame for these fve variables?

colname <- names(murders)
colname
## [1] "state"      "abb"        "region"     "population" "total"

Use the accessor $ 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"

Now use the square brackets to extract the state abbreviations and assign them to the object 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

We saw that the 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

. Use the function c to create a vector with the average high temperatures in January for Beijing, Lagos, Paris, Rio de Janeiro, San Juan and Toronto, which are 35, 88, 42, 84, 81, and 30 degrees Fahrenheit. Call the object temp.

temp <- c(35, 88, 42, 84, 81, 30)
temp
## [1] 35 88 42 84 81 30

Now create a vector with the city names and call the object city.

city <- c("Beijing", "Lagos", "Paris", "Rio de Janeiro", "San Juan", "Toronto")
city
## [1] "Beijing"        "Lagos"          "Paris"          "Rio de Janeiro"
## [5] "San Juan"       "Toronto"

Use the names function and the objects defined in the previous exercises to associate the temperature data with its corresponding city

names(temp) <- city
names(temp)
## [1] "Beijing"        "Lagos"          "Paris"          "Rio de Janeiro"
## [5] "San Juan"       "Toronto"

Use the [ and : operators to access the temperature of the frst three cities on the list.

temp[1:3]
## Beijing   Lagos   Paris 
##      35      88      42

Use the [ operator to access the temperature of Paris and San Juan.

temp[c("Paris", "San Juan")]
##    Paris San Juan 
##       42       81

Use the : 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

Create a vector containing all the positive odd numbers smaller than 100.

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

Create a vector of numbers that starts at 6, does not pass 55, and adds numbers in increments of 4/7: 6, 6+4/7, 6+8/7, etc.. How many numbers does the list have? Hint: use seq and length.

increment_vector <- seq(6, 55, by = 4/7)
length(increment_vector)
## [1] 86

What is the class of the following object a <- seq(1, 10, 0.5) ?

The class of the object a <- seq(1, 10, 0.5) is “numeric”.

What is the class of the following object a <- seq(1, 10) ?

The class of the object a <- seq(1, 10) is “numeric”.

The class of 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.

The class of 1L is “integer.” Adding “L” to a number in R forces it to be an integer.

Define the following vector: 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"