library(dslabs)
## Warning: package 'dslabs' was built under R version 4.2.3
data(murders)

##A.axemine

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 ...

##B.the 51 states

(murders$state)
##  [1] "Alabama"              "Alaska"               "Arizona"             
##  [4] "Arkansas"             "California"           "Colorado"            
##  [7] "Connecticut"          "Delaware"             "District of Columbia"
## [10] "Florida"              "Georgia"              "Hawaii"              
## [13] "Idaho"                "Illinois"             "Indiana"             
## [16] "Iowa"                 "Kansas"               "Kentucky"            
## [19] "Louisiana"            "Maine"                "Maryland"            
## [22] "Massachusetts"        "Michigan"             "Minnesota"           
## [25] "Mississippi"          "Missouri"             "Montana"             
## [28] "Nebraska"             "Nevada"               "New Hampshire"       
## [31] "New Jersey"           "New Mexico"           "New York"            
## [34] "North Carolina"       "North Dakota"         "Ohio"                
## [37] "Oklahoma"             "Oregon"               "Pennsylvania"        
## [40] "Rhode Island"         "South Carolina"       "South Dakota"        
## [43] "Tennessee"            "Texas"                "Utah"                
## [46] "Vermont"              "Virginia"             "Washington"          
## [49] "West Virginia"        "Wisconsin"            "Wyoming"
(murders$total)
##  [1]  135   19  232   93 1257   65   97   38   99  669  376    7   12  364  142
## [16]   21   63  116  351   11  293  118  413   53  120  321   12   32   84    5
## [31]  246   67  517  286    4  310  111   36  457   16  207    8  219  805   22
## [46]    2  250   93   27   97    5

##c.state name and the obbrevation of the state name the stae region and state population and total numbers og murders for 2010

(murders$region)
##  [1] South         West          West          South         West         
##  [6] West          Northeast     South         South         South        
## [11] South         West          West          North Central North Central
## [16] North Central North Central South         South         Northeast    
## [21] South         Northeast     North Central North Central South        
## [26] North Central West          North Central West          Northeast    
## [31] Northeast     West          Northeast     South         North Central
## [36] North Central South         West          Northeast     Northeast    
## [41] South         North Central South         South         West         
## [46] Northeast     South         West          South         North Central
## [51] West         
## Levels: Northeast South North Central West
(murders$total)
##  [1]  135   19  232   93 1257   65   97   38   99  669  376    7   12  364  142
## [16]   21   63  116  351   11  293  118  413   53  120  321   12   32   84    5
## [31]  246   67  517  286    4  310  111   36  457   16  207    8  219  805   22
## [46]    2  250   93   27   97    5
(murders$population)
##  [1]  4779736   710231  6392017  2915918 37253956  5029196  3574097   897934
##  [9]   601723 19687653  9920000  1360301  1567582 12830632  6483802  3046355
## [17]  2853118  4339367  4533372  1328361  5773552  6547629  9883640  5303925
## [25]  2967297  5988927   989415  1826341  2700551  1316470  8791894  2059179
## [33] 19378102  9535483   672591 11536504  3751351  3831074 12702379  1052567
## [41]  4625364   814180  6346105 25145561  2763885   625741  8001024  6724540
## [49]  1852994  5686986   563626
(murders$abb)
##  [1] "AL" "AK" "AZ" "AR" "CA" "CO" "CT" "DE" "DC" "FL" "GA" "HI" "ID" "IL" "IN"
## [16] "IA" "KS" "KY" "LA" "ME" "MD" "MA" "MI" "MN" "MS" "MO" "MT" "NE" "NV" "NH"
## [31] "NJ" "NM" "NY" "NC" "ND" "OH" "OK" "OR" "PA" "RI" "SC" "SD" "TN" "TX" "UT"
## [46] "VT" "VA" "WA" "WV" "WI" "WY"
(murders$state)
##  [1] "Alabama"              "Alaska"               "Arizona"             
##  [4] "Arkansas"             "California"           "Colorado"            
##  [7] "Connecticut"          "Delaware"             "District of Columbia"
## [10] "Florida"              "Georgia"              "Hawaii"              
## [13] "Idaho"                "Illinois"             "Indiana"             
## [16] "Iowa"                 "Kansas"               "Kentucky"            
## [19] "Louisiana"            "Maine"                "Maryland"            
## [22] "Massachusetts"        "Michigan"             "Minnesota"           
## [25] "Mississippi"          "Missouri"             "Montana"             
## [28] "Nebraska"             "Nevada"               "New Hampshire"       
## [31] "New Jersey"           "New Mexico"           "New York"            
## [34] "North Carolina"       "North Dakota"         "Ohio"                
## [37] "Oklahoma"             "Oregon"               "Pennsylvania"        
## [40] "Rhode Island"         "South Carolina"       "South Dakota"        
## [43] "Tennessee"            "Texas"                "Utah"                
## [46] "Vermont"              "Virginia"             "Washington"          
## [49] "West Virginia"        "Wisconsin"            "Wyoming"

##D. str show no relevent information

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 ...

##2.what are the column name used by the data frame for these five variable

colnames(murders)
## [1] "state"      "abb"        "region"     "population" "total"

##3.use the accessor $to extract the state abbrevation and assign them to the object a. what is the class of object

a<-(murders$abb)
a
##  [1] "AL" "AK" "AZ" "AR" "CA" "CO" "CT" "DE" "DC" "FL" "GA" "HI" "ID" "IL" "IN"
## [16] "IA" "KS" "KY" "LA" "ME" "MD" "MA" "MI" "MN" "MS" "MO" "MT" "NE" "NV" "NH"
## [31] "NJ" "NM" "NY" "NC" "ND" "OH" "OK" "OR" "PA" "RI" "SC" "SD" "TN" "TX" "UT"
## [46] "VT" "VA" "WA" "WV" "WI" "WY"
class(a)
## [1] "character"

##4.use the square brackets to extract the stat abbrevation and assign them to object b.

b<-murders[["abbrevation"]]
b
## NULL
a==b
## logical(0)

##5.use saw that the region column store s function you can cororate this by typing

class(murders$region)
## [1] "factor"

##6.The function table takes a vector and returns the frequency of each element. You can quickly see

table(murders$region)
## 
##     Northeast         South North Central          West 
##             9            17            12            13

##Exercise 3.9 ## 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"

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

# Assuming 'temp' and 'city' are already defined
names(temp) <- city
names(temp)
## [1] "Beijing"        "Lagos"          "Paris"          "Rio de Janeiro"
## [5] "San Juan"       "Toronto"

##4.Use the [ and : operators to access the temperature of the first three cities on the list.

# Assuming 'temp' is already defined
first_three_temps <- temp[1:3]
first_three_temps
## Beijing   Lagos   Paris 
##      35      88      42

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

# Assuming 'temp' is already defined
paris_san_juan_temps <- temp[c("Paris", "San Juan")]
paris_san_juan_temps
##    Paris San Juan 
##       42       81

##6.Use the : operator to create a sequence of numbers 12,13,14,…,73

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

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

seq(1,99,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

##8.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?

 seq( 6,  4/7, length.out = 100)
##   [1] 6.0000000 5.9451659 5.8903319 5.8354978 5.7806638 5.7258297 5.6709957
##   [8] 5.6161616 5.5613276 5.5064935 5.4516595 5.3968254 5.3419913 5.2871573
##  [15] 5.2323232 5.1774892 5.1226551 5.0678211 5.0129870 4.9581530 4.9033189
##  [22] 4.8484848 4.7936508 4.7388167 4.6839827 4.6291486 4.5743146 4.5194805
##  [29] 4.4646465 4.4098124 4.3549784 4.3001443 4.2453102 4.1904762 4.1356421
##  [36] 4.0808081 4.0259740 3.9711400 3.9163059 3.8614719 3.8066378 3.7518038
##  [43] 3.6969697 3.6421356 3.5873016 3.5324675 3.4776335 3.4227994 3.3679654
##  [50] 3.3131313 3.2582973 3.2034632 3.1486291 3.0937951 3.0389610 2.9841270
##  [57] 2.9292929 2.8744589 2.8196248 2.7647908 2.7099567 2.6551227 2.6002886
##  [64] 2.5454545 2.4906205 2.4357864 2.3809524 2.3261183 2.2712843 2.2164502
##  [71] 2.1616162 2.1067821 2.0519481 1.9971140 1.9422799 1.8874459 1.8326118
##  [78] 1.7777778 1.7229437 1.6681097 1.6132756 1.5584416 1.5036075 1.4487734
##  [85] 1.3939394 1.3391053 1.2842713 1.2294372 1.1746032 1.1197691 1.0649351
##  [92] 1.0101010 0.9552670 0.9004329 0.8455988 0.7907648 0.7359307 0.6810967
##  [99] 0.6262626 0.5714286
length(sequence)
## [1] 1

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

a <- seq(1, 10, 0.5)
a
##  [1]  1.0  1.5  2.0  2.5  3.0  3.5  4.0  4.5  5.0  5.5  6.0  6.5  7.0  7.5  8.0
## [16]  8.5  9.0  9.5 10.0

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

a <- seq(1, 10)
a
##  [1]  1  2  3  4  5  6  7  8  9 10

##11.The class of class(a<-1) is numeric, not integer. R defaults to numeric and to force an integer, youneed to add the letter L. Confirm that the class of 1L is integer.

class(a <- 1)
## [1] "numeric"
class(b <- 1L)
## [1] "integer"

##12.Define the following vector:x <- c(“1”, “3”, “5”)and coerce it to get integers.

x <- c("1", "3", "5")
x <- as.integer(x)
x
## [1] 1 3 5