Data101_2022W1_Test1soln

pdf

School

University of British Columbia *

*We aren’t endorsed by this school

Course

101

Subject

Mathematics

Date

Feb 20, 2024

Type

pdf

Pages

7

Uploaded by CountBoulder5671

Report
DATA 101 Test 1 Oct 13, 2022 INSTRUCTOR: Lengyi Han / Yas Yamin Irving K. Barber School of Arts and Sciences UBC, Okanagan Campus This exam has 11 questions, for a total of 40 points. The 8th question is optional for 2 bonus marks. READ THE QUESTIONS CAREFULLY In order to get full credit you must SHOW YOUR WORK Answer the questions in the spaces provided on the question sheets. Any calculator is allowed. No textbook, laptops, cell phones, or other electronic devices are permitted. SURNAME, GIVEN NAME (print). STUDENT NUMBER Signature: Question Points Score 1 2 2 10 3 2 4 2 5 3 6 3 7 7 8 1 9 4 10 4 11 2 Total: 40 This exam has 7 pages including this cover page
DATA 101 Test 1 (continued) 1. Circle the correct answers for the following two questions (a) 1 Which command will generate 25 random numbers between 0 and 5 from the uniform distribution? i. runif (25,0,5) ii. runif [0,5,25] iii. runif 25, 0, 5 iv. runif [25,0,5] v. runif (0,5,25) Solution: i (b) 1 I would like to simulate the result of tossing a pair of 6-sided dice. To do this, I can use the following command: i. sample(1:6, size = 2, replace = TRUE) ii. sample(1:2, size = 6, replace = TRUE) iii. sample(1:2, size = 6, replace = FALSE) iv. sample(1:6, size = 2, replace = FALSE) Solution: ii 2. What would you expect the R output to be in response to the following commands: (a) 1 15 %% 6 (b) 2 seq ( 2 , 6 , 2 ) ^ ( 3 : 1 ) (c) 1 10 ^ 50 + 17 - 10 ^ 50 Page 2 of 7
DATA 101 Test 1 (continued) (d) 1 10 ^ 50 - 10 ^ 50 - 17 (e) 1 rep ( 1 : 4 , 1 : 4 ) (f) 2 y <- factor ( c ( 1 , 3 , 1 , 1 , 1 , 3 )) levels (y) <- c ( "A" , "H" ) y[ c ( 6 , 1 , 2 , 3 )] (g) 1 y <- c ( 3 , 6 , 1 , 2 , 0 , 5 , 2 ) y[y > 3 ] (h) 1 1 : 3 * 2 - 1 : ( 3 * 2 ) Page 3 of 7
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
DATA 101 Test 1 (continued) Solution: ## [1] 3 ## [1] 8 16 6 ## [1] 0 ## [1] -17 ## [1] 1 2 2 3 3 3 4 4 4 4 ## [1] H A H A ## Levels: A H ## [1] 6 5 ## [1] 1 2 3 -2 -1 0 3. 2 The surface area, SA, of a sphere of radius r is given by SA = 4 πr 2 . Write the lines of R code which could be used to calculate the approximate surface area of Earth, assuming a radius of 6378 km. The code should result in the assignment of the surface area value to an object named AreaEarth . Solution: r <- 6378 AreaEarth <- 4 * pi * r ^ 2 4. 2 Convert the following binary number to its decimal representation: 10 . 11 2 Solution: 4.75 5. Convert the following decimal numbers to their binary representations. (a) 1 23 (b) 2 1.625 Solution: 10111 2 1 . 101 2 Page 4 of 7
DATA 101 Test 1 (continued) 6. (a) 2 Use the rep and seq functions to create the following vector ## [1] 2 4 6 8 10 2 4 6 8 10 2 4 6 8 10 2 4 6 8 10 2 4 6 8 10 assigning it to an object called x . (b) 1 Write down the R code to calculate the pairwise minima of the two vectors: x [1] 12 16 18 18 22 y [1] 11 15 19 23 27 Solution: rep ( seq ( 2 , 10 , 2 ), 5 ) pmin (x,y) 7. In this question, you will follow the steps below to write R code to determine which is the largest of ( 100 j =1 j ) 2 and ( 100 j =1 j 3 ) 1 / 3 . Specifically, write the R code to (a) 1 create a vector called numbers that contains the integers from 1 through 100. (b) 2 create two new vectors, called numsqrt and numcube which contain the square roots and cubes of the numbers in numbers , respectively. (c) 2 find the sums of the 2 vectors obtained in the previous part. Then calculate the second power of the first sum and the cube root of the second sum. (d) 2 determine which of the two quantities obtained in the previous part is the largest? Solution: numbers <- 1 : 100 numsqrt <- sqrt (numbers) numcube <- numbers ^ 3 sum (numsqrt) ^ 2 ; sum (numcube) ^ ( 1 / 3 ) ## [1] 450862.489 ## [1] 294.347884 max ( sum (numsqrt) ^ 2 , sum (numcube) ^ ( 1 / 3 )) ## [1] 450862.489 Page 5 of 7
DATA 101 Test 1 (continued) 8. 1 The music preferences for a class of 50 university students are contained in a vector called music , displayed below. Fill in the blank in the R code (below) which would produce output showing the numbers of students favoring each type of music. ## [1] "alternative" "country" "alternative" "alternative" "country" ## [6] "rock" "hip-hop" "alternative" "hip-hop" "rock" ## [11] "rock" "country" "country" "rock" "alternative" ## [16] "alternative" "alternative" "country" "rock" "rock" ## [21] "rock" "hip-hop" "alternative" "rock" "country" ## [26] "country" "rock" "alternative" "alternative" "country" ## [31] "alternative" "alternative" "alternative" "rock" "hip-hop" ## [36] "alternative" "alternative" "alternative" "country" "country" ## [41] "rock" "rock" "alternative" "rock" "country" ## [46] "alternative" "alternative" "alternative" "rock" "alternative" __________ (music) Solution: table (music) 9. Consider the following vector in R: x <- c ( 1 , 7 , NA , 2 , 4 , 3 , NA , 5 , NA , 6 ) (a) 1 Write an R command which returns the positions of the missing values in x. (b) 1 Write an R command which returns the number of non-missing values in x. (c) 2 Write an R program which replaces the missing values in x with the mean of the non- missing values in x. Solution: #a which ( is.na (x)) ## [1] 3 7 9 #b x[ ! is.na (x)] ## [1] 1 7 2 4 3 5 6 #or x[ - which ( is.na (x))] ## [1] 1 7 2 4 3 5 6 #c x[ which ( is.na (x))] <- mean (x[ ! is.na (x)]) x ## [1] 1 7 4 2 4 3 4 5 4 6 Page 6 of 7
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
DATA 101 Test 1 (continued) 10. Suppose the data frame my.df has been entered into an R workspace, and its contents are as follows. ## name height owncar occupation ## 1 Ann 170 TRUE engineer ## 2 Bob 180 TRUE lawyer ## 3 Carl 175 FALSE engineer ## 4 Dave 170 FALSE lawyer ## 5 Emma 172 TRUE engineer ## 6 Fran 167 TRUE lawyer ## 7 Gary 174 FALSE engineer What is printed to the screen when the following commands are executed? (a) 1 my.df[ 3 , 2 ] Solution: ## [1] 175 (b) 1 subset (my.df, occupation == "lawyer" ) $ name Solution: ## [1] "Bob" "Dave" "Fran" (c) 1 subset (my.df, (occupation == "lawyer" ) & (height > 179 )) Solution: ## name height owncar occupation ## 2 Bob 180 TRUE lawyer (d) 1 my.df $ height[my.df $ owncar] Solution: ## [1] 170 180 172 167 11. 2 What would you expect the R output to be in response to the following commands: x <- "The" y <- "End" paste ( paste (x, y), "!" , sep = ":-)" ) Solution: ## [1] "The End:-)!" Page 7 of 7