├── README.md ├── Introduction to R.Rmd └── Introduction to R.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # R Programming 2 | ### Instructor: Sana Rasheed 3 | 4 | R Programming course offered by Al-Nafi. 5 | 6 | This repository contains practise exercises for students in .ipynb and .Rmd formats. 7 | -------------------------------------------------------------------------------- /Introduction to R.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Introduction to R Programming" 3 | author: "Sana Rasheed" 4 | date: "May 14, 2020" 5 | output: html_document 6 | --- 7 | 8 | 9 | ##### NOTE: 10 | 1. Change author name and date to your exercise submission date in above section 11 | 2. Your code MUST execute without any errors. 12 | 3. You can add more lines in your code as required. 13 | 14 | ## Section 1: Data Types and Operations Pt. 1 15 | 16 | ### Question 1 17 | **Create the variables with the following composition:** 18 | 1. A vector containing each letter of your first name as its elements. 19 | 2. A variable that contains your name concatenated from the vector created in (1) 20 | 3. A variable containing a sequence from 100 to 120. 21 | 4. Create a matrix of 3x3 dimensions that contains the even sequence of numbers starting from 2. 22 | 5. Assign names to the variables. 23 | 24 | 25 | ```{r } 26 | #### Start solution #### 27 | ``` 28 | 29 | 30 | ```{r } 31 | 32 | ``` 33 | 34 | 35 | ```{r } 36 | 37 | ``` 38 | 39 | 40 | ```{r } 41 | 42 | ``` 43 | 44 | 45 | ```{r } 46 | 47 | ``` 48 | 49 | 50 | ```{r } 51 | #### End solution #### 52 | ``` 53 | 54 | ### Question 2 55 | **Create a factor variable emp_status:** 56 | 1. Containing the categorical variables: Employed, Unemployed, Self-Employed, with each level appearing atleast more than 2. 57 | 2. Display the levels and the factor variable as a table. 58 | 3. Unclass the elements of the factor variable. 59 | 60 | 61 | ```{r } 62 | #### Start solution #### 63 | ``` 64 | 65 | 66 | ```{r } 67 | 68 | ``` 69 | 70 | 71 | ```{r } 72 | 73 | ``` 74 | 75 | 76 | ```{r } 77 | 78 | ``` 79 | 80 | 81 | ```{r } 82 | #### End solution #### 83 | ``` 84 | 85 | ### Question 3 86 | **Create a dataframe object called bank_customers:** 87 | 1. the data frame will have three columns: CustomerID, hasAccount, totalBalance 88 | 2. Fill the data as follows 89 | a. Alicia does not have an account. She is here for inquiry 90 | b. Nancy is here to check on her account balance of USD 10,000. 91 | c. Fernando is here to deposit USD 100 in his account which had no balance. 92 | d. Louis will withdraw all his money from the account that had USD 2,000. 93 | e. Diane is here for information. 94 | 3. For customers that do not have a value, use NA as placeholder. 95 | 4. Print the number of rows, columns and names for the data frame. 96 | 97 | 98 | ```{r } 99 | #### Start solution #### 100 | ``` 101 | 102 | 103 | ```{r } 104 | 105 | ``` 106 | 107 | 108 | ```{r } 109 | 110 | ``` 111 | 112 | 113 | ```{r } 114 | 115 | ``` 116 | 117 | 118 | ```{r } 119 | 120 | ``` 121 | 122 | 123 | ```{r } 124 | #### End solution #### 125 | ``` 126 | 127 | ### Good Job! You have successfully completed the section! 128 | 129 | ## Section 2: Control Structures 130 | 131 | ### Question 1 132 | **Create a variable containing a sequence of numbers from 1 to 100:** 133 | 1. Iterate over the variables and print those numbers which are prime. 134 | 135 | **Create a variable var with a value of 1:** 136 | 1. Create a while loop and increase the value of var at each iteration. 137 | 2. When you reach the 10th prime number, terminate the loop and print the number. 138 | 139 | 140 | ```{r } 141 | #### Start solution #### 142 | ``` 143 | 144 | 145 | ```{r } 146 | 147 | ``` 148 | 149 | 150 | ```{r } 151 | 152 | ``` 153 | 154 | 155 | ```{r } 156 | 157 | ``` 158 | 159 | 160 | ```{r } 161 | 162 | ``` 163 | 164 | 165 | ```{r } 166 | 167 | ``` 168 | 169 | 170 | ```{r } 171 | #### End solution #### 172 | ``` 173 | 174 | ### Question 2 175 | **Create a matrix of size 3x3 called mat_1:** 176 | 1. Iterate over all the values one by one and print the element as well as the position in the matrix (row, col) 177 | 178 | 179 | ```{r } 180 | #### Start solution #### 181 | ``` 182 | 183 | 184 | ```{r } 185 | 186 | ``` 187 | 188 | 189 | ```{r } 190 | 191 | ``` 192 | 193 | 194 | ```{r } 195 | 196 | ``` 197 | 198 | 199 | ```{r } 200 | 201 | ``` 202 | 203 | 204 | ```{r } 205 | 206 | ``` 207 | 208 | 209 | ```{r } 210 | #### End solution #### 211 | ``` 212 | 213 | ### Good Job! You have successfully completed the section! 214 | 215 | ## Section 3: Functions 216 | 217 | ### Question 1 218 | **Create a function called gcd that finds the greatest common divisor of two numbers a and b:** 219 | 1. a and b, should be taken as input. 220 | 2. The function must print the GCD as well as return it. 221 | 3. The output must be saved in a variable called answer. 222 | 223 | 224 | ```{r } 225 | #### Start solution #### 226 | ``` 227 | 228 | 229 | ```{r } 230 | 231 | ``` 232 | 233 | 234 | ```{r } 235 | 236 | ``` 237 | 238 | 239 | ```{r } 240 | 241 | ``` 242 | 243 | 244 | ```{r } 245 | 246 | ``` 247 | 248 | 249 | ```{r } 250 | 251 | ``` 252 | 253 | 254 | ```{r } 255 | #### End solution #### 256 | ``` 257 | 258 | ### Question 2 259 | **Create a function called allConditionsMeet, that checks whether two expressions evaluate to true:** 260 | 1. a and b, should be taken as input. 261 | 2. the function should check if a and b, both conditions, evaluate to True. 262 | 3. The function must returns a boolean value. 263 | 4. Using a method, print the arguments that function takes. 264 | 265 | 266 | ```{r } 267 | #### Start solution #### 268 | ``` 269 | 270 | 271 | ```{r } 272 | 273 | ``` 274 | 275 | 276 | ```{r } 277 | 278 | ``` 279 | 280 | 281 | ```{r } 282 | 283 | ``` 284 | 285 | 286 | ```{r } 287 | 288 | ``` 289 | 290 | 291 | ```{r } 292 | 293 | ``` 294 | 295 | 296 | ```{r } 297 | #### End solution #### 298 | ``` 299 | 300 | ### Good Job! You have successfully completed the section! 301 | 302 | ## Section 4: Vectorized Operations 303 | 304 | ### Question 1 305 | **Create two matrices matrix_1 and matrix_2 of dimensions 2x3 and 3x2:** 306 | 1. Perform element-wise multiplication. 307 | 2. Perform matrix multipilcation. 308 | 309 | **Create a 2x2 matrix my_mat:** 310 | 1. Write a function to find the determinant of the matrix. 311 | 312 | 313 | ```{r } 314 | #### Start solution #### 315 | ``` 316 | 317 | 318 | ```{r } 319 | 320 | ``` 321 | 322 | 323 | ```{r } 324 | 325 | ``` 326 | 327 | 328 | ```{r } 329 | 330 | ``` 331 | 332 | 333 | ```{r } 334 | 335 | ``` 336 | 337 | 338 | ```{r } 339 | 340 | ``` 341 | 342 | 343 | ```{r } 344 | #### End solution #### 345 | ``` 346 | 347 | ### Good Job! You have successfully completed the section! 348 | 349 | ## Section 5: Date and Time in R 350 | 351 | ### Question 1 352 | **Use the current date and time and store them into variables curr_date and curr_time respectively: [use sys]** 353 | 1. Convert both into date and time objects using the appropriate functions. 354 | 2. Print the weekday, year, second and hour using the appropriate function and variables. 355 | 356 | 357 | ```{r } 358 | #### Start solution #### 359 | ``` 360 | 361 | 362 | ```{r } 363 | 364 | ``` 365 | 366 | 367 | ```{r } 368 | 369 | ``` 370 | 371 | 372 | ```{r } 373 | 374 | ``` 375 | 376 | 377 | ```{r } 378 | 379 | ``` 380 | 381 | 382 | ```{r } 383 | 384 | ``` 385 | 386 | 387 | ```{r } 388 | #### End solution #### 389 | ``` 390 | 391 | ### Question 2 392 | **Create a variable to store current date/time** 393 | 1. Create another variable that stores and set the timezone as GMT-5 394 | 2. Find the difference between your current time and the GMT-5 timezone. 395 | 396 | 397 | ```{r } 398 | #### Start solution #### 399 | ``` 400 | 401 | 402 | ```{r } 403 | 404 | ``` 405 | 406 | 407 | ```{r } 408 | 409 | ``` 410 | 411 | 412 | ```{r } 413 | 414 | ``` 415 | 416 | 417 | ```{r } 418 | 419 | ``` 420 | 421 | 422 | ```{r } 423 | 424 | ``` 425 | 426 | 427 | ```{r } 428 | #### End solution #### 429 | ``` 430 | 431 | ### Good Job! You have successfully completed the section! 432 | 433 | ## Section 6: Loop Functions 434 | 435 | ### Question 1 436 | **Create a function to calculate mean and standard deviation of the provided data** 437 | 1. Create a sequence of numbers from 100 to 150 store in a variable called numbers. 438 | 1. Use lapply, sapply, apply and tapply to implement the functions on "numbers" [only on the second half of the sequence for tapply] 439 | 440 | 441 | ```{r } 442 | #### Start solution #### 443 | ``` 444 | 445 | 446 | ```{r } 447 | 448 | ``` 449 | 450 | 451 | ```{r } 452 | 453 | ``` 454 | 455 | 456 | ```{r } 457 | 458 | ``` 459 | 460 | 461 | ```{r } 462 | 463 | ``` 464 | 465 | 466 | ```{r } 467 | 468 | ``` 469 | 470 | 471 | ```{r } 472 | #### End solution #### 473 | ``` 474 | 475 | ### Question 2 476 | **Create a matrix of dimensions 4x4** 477 | 1. Find the row-wise and column-wise mean of the matrix. 478 | 2. Print the values. 479 | 480 | 481 | ```{r } 482 | #### Start solution #### 483 | ``` 484 | 485 | 486 | ```{r } 487 | 488 | ``` 489 | 490 | 491 | ```{r } 492 | 493 | ``` 494 | 495 | 496 | ```{r } 497 | 498 | ``` 499 | 500 | 501 | ```{r } 502 | 503 | ``` 504 | 505 | 506 | ```{r } 507 | 508 | ``` 509 | 510 | 511 | ```{r } 512 | #### End solution #### 513 | ``` 514 | 515 | ### Good Job! You have successfully completed the section! 516 | 517 | ## Section 7: Data Split 518 | 519 | ### Question 1 520 | **Using the data frame Orange:** 521 | 1. Using split function to break down the dataset on circumference and store it in 'split_data' variable. 522 | 2. Print the values for split_data where circumference is 30 and 75. 523 | 3. Find the average age of the tree when the circumference is 30 and when circumference is 214. 524 | 525 | 526 | The dataset is loaded and the variable Orange contains the respective dataset. 527 | 528 | 529 | ```{r } 530 | library(datasets) 531 | ``` 532 | 533 | 534 | ```{r } 535 | head(Orange) 536 | ``` 537 | 538 | 539 | ```{r } 540 | #### Start solution #### 541 | ``` 542 | 543 | 544 | ```{r } 545 | 546 | ``` 547 | 548 | 549 | ```{r } 550 | 551 | ``` 552 | 553 | 554 | ```{r } 555 | 556 | ``` 557 | 558 | 559 | ```{r } 560 | 561 | ``` 562 | 563 | 564 | ```{r } 565 | 566 | ``` 567 | 568 | 569 | ```{r } 570 | #### End solution #### 571 | ``` 572 | 573 | ### Save it and push .Rmd and .html file to your Github Repository 574 | ## GOOD JOB! You have successfully finished the course! -------------------------------------------------------------------------------- /Introduction to R.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Course - Introduction to R " 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "##### NOTE: \n", 15 | "1. Your code MUST execute without any errors. \n", 16 | "2. You can add more lines in your code as required." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "## Section 1: Data Types and Operations Pt. 1" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "### Question 1 \n", 31 | "**Create the variables with the following composition:**\n", 32 | "1. A vector containing each letter of your first name as its elements.\n", 33 | "2. A variable that contains your name concatenated from the vector created in (1)\n", 34 | "3. A variable containing a sequence from 100 to 120.\n", 35 | "4. Create a matrix of 3x3 dimensions that contains the even sequence of numbers starting from 2.\n", 36 | "5. Assign names to the variables." 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [ 45 | "#### Start solution ####" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": null, 79 | "metadata": {}, 80 | "outputs": [], 81 | "source": [ 82 | "#### End solution ####" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "### Question 2\n", 90 | "**Create a factor variable emp_status:**\n", 91 | "1. Containing the categorical variables: Employed, Unemployed, Self-Employed, with each level appearing atleast more than 2.\n", 92 | "2. Display the levels and the factor variable as a table.\n", 93 | "3. Unclass the elements of the factor variable." 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 4, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "#### Start solution ####" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "metadata": {}, 109 | "outputs": [], 110 | "source": [] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": null, 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": null, 122 | "metadata": {}, 123 | "outputs": [], 124 | "source": [] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 3, 129 | "metadata": {}, 130 | "outputs": [], 131 | "source": [ 132 | "#### End solution ####" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "### Question 3\n", 140 | "**Create a dataframe object called bank_customers:**\n", 141 | "1. the data frame will have three columns: CustomerID, hasAccount, totalBalance\n", 142 | "2. Fill the data as follows\n", 143 | " a. Alicia does not have an account. She is here for inquiry \n", 144 | " b. Nancy is here to check on her account balance of USD 10,000.\n", 145 | " c. Fernando is here to deposit USD 100 in his account which had no balance.\n", 146 | " d. Louis will withdraw all his money from the account that had USD 2,000. \n", 147 | " e. Diane is here for information. \n", 148 | "3. For customers that do not have a value, use NA as placeholder.\n", 149 | "4. Print the number of rows, columns and names for the data frame." 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": null, 155 | "metadata": {}, 156 | "outputs": [], 157 | "source": [ 158 | "#### Start solution ####" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": null, 164 | "metadata": {}, 165 | "outputs": [], 166 | "source": [] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": null, 171 | "metadata": {}, 172 | "outputs": [], 173 | "source": [] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": null, 178 | "metadata": {}, 179 | "outputs": [], 180 | "source": [] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": null, 185 | "metadata": {}, 186 | "outputs": [], 187 | "source": [] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": 2, 192 | "metadata": {}, 193 | "outputs": [], 194 | "source": [ 195 | "#### End solution ####" 196 | ] 197 | }, 198 | { 199 | "cell_type": "markdown", 200 | "metadata": {}, 201 | "source": [ 202 | "### Good Job! You have successfully completed the section!" 203 | ] 204 | }, 205 | { 206 | "cell_type": "markdown", 207 | "metadata": {}, 208 | "source": [ 209 | "## Section 2: Control Structures " 210 | ] 211 | }, 212 | { 213 | "cell_type": "markdown", 214 | "metadata": {}, 215 | "source": [ 216 | "### Question 1\n", 217 | "**Create a variable containing a sequence of numbers from 1 to 100:**\n", 218 | "1. Iterate over the variables and print those numbers which are prime.\n", 219 | "\n", 220 | "**Create a variable var with a value of 1:**\n", 221 | "1. Create a while loop and increase the value of var at each iteration. \n", 222 | "2. When you reach the 10th prime number, terminate the loop and print the number." 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": 5, 228 | "metadata": {}, 229 | "outputs": [], 230 | "source": [ 231 | "#### Start solution ####" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": null, 237 | "metadata": {}, 238 | "outputs": [], 239 | "source": [] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": null, 244 | "metadata": {}, 245 | "outputs": [], 246 | "source": [] 247 | }, 248 | { 249 | "cell_type": "code", 250 | "execution_count": null, 251 | "metadata": {}, 252 | "outputs": [], 253 | "source": [] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": null, 258 | "metadata": {}, 259 | "outputs": [], 260 | "source": [] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": null, 265 | "metadata": {}, 266 | "outputs": [], 267 | "source": [] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": 6, 272 | "metadata": {}, 273 | "outputs": [], 274 | "source": [ 275 | "#### End solution ####" 276 | ] 277 | }, 278 | { 279 | "cell_type": "markdown", 280 | "metadata": {}, 281 | "source": [ 282 | "### Question 2\n", 283 | "**Create a matrix of size 3x3 called mat_1:**\n", 284 | "1. Iterate over all the values one by one and print the element as well as the position in the matrix (row, col)" 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": 9, 290 | "metadata": {}, 291 | "outputs": [], 292 | "source": [ 293 | "#### Start solution ####" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": null, 299 | "metadata": {}, 300 | "outputs": [], 301 | "source": [] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "execution_count": null, 306 | "metadata": {}, 307 | "outputs": [], 308 | "source": [] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": null, 313 | "metadata": {}, 314 | "outputs": [], 315 | "source": [] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": null, 320 | "metadata": {}, 321 | "outputs": [], 322 | "source": [] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": null, 327 | "metadata": {}, 328 | "outputs": [], 329 | "source": [] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": 14, 334 | "metadata": {}, 335 | "outputs": [], 336 | "source": [ 337 | "#### End solution ####" 338 | ] 339 | }, 340 | { 341 | "cell_type": "markdown", 342 | "metadata": {}, 343 | "source": [ 344 | "### Good Job! You have successfully completed the section!" 345 | ] 346 | }, 347 | { 348 | "cell_type": "markdown", 349 | "metadata": {}, 350 | "source": [ 351 | "## Section 3: Functions" 352 | ] 353 | }, 354 | { 355 | "cell_type": "markdown", 356 | "metadata": {}, 357 | "source": [ 358 | "### Question 1\n", 359 | "**Create a function called gcd that finds the greatest common divisor of two numbers a and b:**\n", 360 | "1. a and b, should be taken as input.\n", 361 | "2. The function must print the GCD as well as return it.\n", 362 | "3. The output must be saved in a variable called answer." 363 | ] 364 | }, 365 | { 366 | "cell_type": "code", 367 | "execution_count": 9, 368 | "metadata": {}, 369 | "outputs": [], 370 | "source": [ 371 | "#### Start solution ####" 372 | ] 373 | }, 374 | { 375 | "cell_type": "code", 376 | "execution_count": null, 377 | "metadata": {}, 378 | "outputs": [], 379 | "source": [] 380 | }, 381 | { 382 | "cell_type": "code", 383 | "execution_count": null, 384 | "metadata": {}, 385 | "outputs": [], 386 | "source": [] 387 | }, 388 | { 389 | "cell_type": "code", 390 | "execution_count": null, 391 | "metadata": {}, 392 | "outputs": [], 393 | "source": [] 394 | }, 395 | { 396 | "cell_type": "code", 397 | "execution_count": null, 398 | "metadata": {}, 399 | "outputs": [], 400 | "source": [] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "execution_count": null, 405 | "metadata": {}, 406 | "outputs": [], 407 | "source": [] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": 14, 412 | "metadata": {}, 413 | "outputs": [], 414 | "source": [ 415 | "#### End solution ####" 416 | ] 417 | }, 418 | { 419 | "cell_type": "markdown", 420 | "metadata": {}, 421 | "source": [ 422 | "### Question 2\n", 423 | "**Create a function called allConditionsMeet, that checks whether two expressions evaluate to true:**\n", 424 | "1. a and b, should be taken as input.\n", 425 | "2. the function should check if a and b, both conditions, evaluate to True.\n", 426 | "3. The function must returns a boolean value.\n", 427 | "4. Using a method, print the arguments that function takes." 428 | ] 429 | }, 430 | { 431 | "cell_type": "code", 432 | "execution_count": 9, 433 | "metadata": {}, 434 | "outputs": [], 435 | "source": [ 436 | "#### Start solution ####" 437 | ] 438 | }, 439 | { 440 | "cell_type": "code", 441 | "execution_count": null, 442 | "metadata": {}, 443 | "outputs": [], 444 | "source": [] 445 | }, 446 | { 447 | "cell_type": "code", 448 | "execution_count": null, 449 | "metadata": {}, 450 | "outputs": [], 451 | "source": [] 452 | }, 453 | { 454 | "cell_type": "code", 455 | "execution_count": null, 456 | "metadata": {}, 457 | "outputs": [], 458 | "source": [] 459 | }, 460 | { 461 | "cell_type": "code", 462 | "execution_count": null, 463 | "metadata": {}, 464 | "outputs": [], 465 | "source": [] 466 | }, 467 | { 468 | "cell_type": "code", 469 | "execution_count": null, 470 | "metadata": {}, 471 | "outputs": [], 472 | "source": [] 473 | }, 474 | { 475 | "cell_type": "code", 476 | "execution_count": 14, 477 | "metadata": {}, 478 | "outputs": [], 479 | "source": [ 480 | "#### End solution ####" 481 | ] 482 | }, 483 | { 484 | "cell_type": "markdown", 485 | "metadata": {}, 486 | "source": [ 487 | "### Good Job! You have successfully completed the section!" 488 | ] 489 | }, 490 | { 491 | "cell_type": "markdown", 492 | "metadata": {}, 493 | "source": [ 494 | "## Section 4: Vectorized Operations" 495 | ] 496 | }, 497 | { 498 | "cell_type": "markdown", 499 | "metadata": {}, 500 | "source": [ 501 | "### Question 1\n", 502 | "**Create two matrices matrix_1 and matrix_2 of dimensions 2x3 and 3x2:**\n", 503 | "1. Perform element-wise multiplication.\n", 504 | "2. Perform matrix multipilcation. \n", 505 | "\n", 506 | "**Create a 2x2 matrix my_mat:**\n", 507 | "1. Write a function to find the determinant of the matrix." 508 | ] 509 | }, 510 | { 511 | "cell_type": "code", 512 | "execution_count": 9, 513 | "metadata": {}, 514 | "outputs": [], 515 | "source": [ 516 | "#### Start solution ####" 517 | ] 518 | }, 519 | { 520 | "cell_type": "code", 521 | "execution_count": null, 522 | "metadata": {}, 523 | "outputs": [], 524 | "source": [] 525 | }, 526 | { 527 | "cell_type": "code", 528 | "execution_count": null, 529 | "metadata": {}, 530 | "outputs": [], 531 | "source": [] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "execution_count": null, 536 | "metadata": {}, 537 | "outputs": [], 538 | "source": [] 539 | }, 540 | { 541 | "cell_type": "code", 542 | "execution_count": null, 543 | "metadata": {}, 544 | "outputs": [], 545 | "source": [] 546 | }, 547 | { 548 | "cell_type": "code", 549 | "execution_count": null, 550 | "metadata": {}, 551 | "outputs": [], 552 | "source": [] 553 | }, 554 | { 555 | "cell_type": "code", 556 | "execution_count": 14, 557 | "metadata": {}, 558 | "outputs": [], 559 | "source": [ 560 | "#### End solution ####" 561 | ] 562 | }, 563 | { 564 | "cell_type": "markdown", 565 | "metadata": {}, 566 | "source": [ 567 | "### Good Job! You have successfully completed the section!" 568 | ] 569 | }, 570 | { 571 | "cell_type": "markdown", 572 | "metadata": {}, 573 | "source": [ 574 | "## Section 5: Date and Time in R" 575 | ] 576 | }, 577 | { 578 | "cell_type": "markdown", 579 | "metadata": {}, 580 | "source": [ 581 | "### Question 1\n", 582 | "**Use the current date and time and store them into variables curr_date and curr_time respectively: [use sys]**\n", 583 | "1. Convert both into date and time objects using the appropriate functions.\n", 584 | "2. Print the weekday, year, second and hour using the appropriate function and variables." 585 | ] 586 | }, 587 | { 588 | "cell_type": "code", 589 | "execution_count": 9, 590 | "metadata": {}, 591 | "outputs": [], 592 | "source": [ 593 | "#### Start solution ####" 594 | ] 595 | }, 596 | { 597 | "cell_type": "code", 598 | "execution_count": null, 599 | "metadata": {}, 600 | "outputs": [], 601 | "source": [] 602 | }, 603 | { 604 | "cell_type": "code", 605 | "execution_count": null, 606 | "metadata": {}, 607 | "outputs": [], 608 | "source": [] 609 | }, 610 | { 611 | "cell_type": "code", 612 | "execution_count": null, 613 | "metadata": {}, 614 | "outputs": [], 615 | "source": [] 616 | }, 617 | { 618 | "cell_type": "code", 619 | "execution_count": null, 620 | "metadata": {}, 621 | "outputs": [], 622 | "source": [] 623 | }, 624 | { 625 | "cell_type": "code", 626 | "execution_count": null, 627 | "metadata": {}, 628 | "outputs": [], 629 | "source": [] 630 | }, 631 | { 632 | "cell_type": "code", 633 | "execution_count": 14, 634 | "metadata": {}, 635 | "outputs": [], 636 | "source": [ 637 | "#### End solution ####" 638 | ] 639 | }, 640 | { 641 | "cell_type": "markdown", 642 | "metadata": {}, 643 | "source": [ 644 | "### Question 2\n", 645 | "**Create a variable to store current date/time**\n", 646 | "1. Create another variable that stores and set the timezone as GMT-5\n", 647 | "2. Find the difference between your current time and the GMT-5 timezone." 648 | ] 649 | }, 650 | { 651 | "cell_type": "code", 652 | "execution_count": 9, 653 | "metadata": {}, 654 | "outputs": [], 655 | "source": [ 656 | "#### Start solution ####" 657 | ] 658 | }, 659 | { 660 | "cell_type": "code", 661 | "execution_count": null, 662 | "metadata": {}, 663 | "outputs": [], 664 | "source": [] 665 | }, 666 | { 667 | "cell_type": "code", 668 | "execution_count": null, 669 | "metadata": {}, 670 | "outputs": [], 671 | "source": [] 672 | }, 673 | { 674 | "cell_type": "code", 675 | "execution_count": null, 676 | "metadata": {}, 677 | "outputs": [], 678 | "source": [] 679 | }, 680 | { 681 | "cell_type": "code", 682 | "execution_count": null, 683 | "metadata": {}, 684 | "outputs": [], 685 | "source": [] 686 | }, 687 | { 688 | "cell_type": "code", 689 | "execution_count": null, 690 | "metadata": {}, 691 | "outputs": [], 692 | "source": [] 693 | }, 694 | { 695 | "cell_type": "code", 696 | "execution_count": 14, 697 | "metadata": {}, 698 | "outputs": [], 699 | "source": [ 700 | "#### End solution ####" 701 | ] 702 | }, 703 | { 704 | "cell_type": "markdown", 705 | "metadata": {}, 706 | "source": [ 707 | "### Good Job! You have successfully completed the section!" 708 | ] 709 | }, 710 | { 711 | "cell_type": "markdown", 712 | "metadata": {}, 713 | "source": [ 714 | "## Section 6: Loop Functions" 715 | ] 716 | }, 717 | { 718 | "cell_type": "markdown", 719 | "metadata": {}, 720 | "source": [ 721 | "### Question 1\n", 722 | "**Create a function to calculate mean and standard deviation of the provided data**\n", 723 | "1. Create a sequence of numbers from 100 to 150 store in a variable called numbers.\n", 724 | "1. Use lapply, sapply, apply and tapply to implement the functions on \"numbers\" [only on the second half of the sequence for tapply]" 725 | ] 726 | }, 727 | { 728 | "cell_type": "code", 729 | "execution_count": 9, 730 | "metadata": {}, 731 | "outputs": [], 732 | "source": [ 733 | "#### Start solution ####" 734 | ] 735 | }, 736 | { 737 | "cell_type": "code", 738 | "execution_count": null, 739 | "metadata": {}, 740 | "outputs": [], 741 | "source": [] 742 | }, 743 | { 744 | "cell_type": "code", 745 | "execution_count": null, 746 | "metadata": {}, 747 | "outputs": [], 748 | "source": [] 749 | }, 750 | { 751 | "cell_type": "code", 752 | "execution_count": null, 753 | "metadata": {}, 754 | "outputs": [], 755 | "source": [] 756 | }, 757 | { 758 | "cell_type": "code", 759 | "execution_count": null, 760 | "metadata": {}, 761 | "outputs": [], 762 | "source": [] 763 | }, 764 | { 765 | "cell_type": "code", 766 | "execution_count": null, 767 | "metadata": {}, 768 | "outputs": [], 769 | "source": [] 770 | }, 771 | { 772 | "cell_type": "code", 773 | "execution_count": 14, 774 | "metadata": {}, 775 | "outputs": [], 776 | "source": [ 777 | "#### End solution ####" 778 | ] 779 | }, 780 | { 781 | "cell_type": "markdown", 782 | "metadata": {}, 783 | "source": [ 784 | "### Question 2\n", 785 | "**Create a matrix of dimensions 4x4**\n", 786 | "1. Find the row-wise and column-wise mean of the matrix.\n", 787 | "2. Print the values." 788 | ] 789 | }, 790 | { 791 | "cell_type": "code", 792 | "execution_count": 9, 793 | "metadata": {}, 794 | "outputs": [], 795 | "source": [ 796 | "#### Start solution ####" 797 | ] 798 | }, 799 | { 800 | "cell_type": "code", 801 | "execution_count": null, 802 | "metadata": {}, 803 | "outputs": [], 804 | "source": [] 805 | }, 806 | { 807 | "cell_type": "code", 808 | "execution_count": null, 809 | "metadata": {}, 810 | "outputs": [], 811 | "source": [] 812 | }, 813 | { 814 | "cell_type": "code", 815 | "execution_count": null, 816 | "metadata": {}, 817 | "outputs": [], 818 | "source": [] 819 | }, 820 | { 821 | "cell_type": "code", 822 | "execution_count": null, 823 | "metadata": {}, 824 | "outputs": [], 825 | "source": [] 826 | }, 827 | { 828 | "cell_type": "code", 829 | "execution_count": null, 830 | "metadata": {}, 831 | "outputs": [], 832 | "source": [] 833 | }, 834 | { 835 | "cell_type": "code", 836 | "execution_count": 14, 837 | "metadata": {}, 838 | "outputs": [], 839 | "source": [ 840 | "#### End solution ####" 841 | ] 842 | }, 843 | { 844 | "cell_type": "markdown", 845 | "metadata": {}, 846 | "source": [ 847 | "### Good Job! You have successfully completed the section!" 848 | ] 849 | }, 850 | { 851 | "cell_type": "markdown", 852 | "metadata": {}, 853 | "source": [ 854 | "## Section 7: Data Split" 855 | ] 856 | }, 857 | { 858 | "cell_type": "markdown", 859 | "metadata": {}, 860 | "source": [ 861 | "### Question 1\n", 862 | "**Using the data frame Orange:**\n", 863 | "1. Using split function to break down the dataset on circumference and store it in 'split_data' variable. \n", 864 | "2. Print the values for split_data where circumference is 30 and 75.\n", 865 | "3. Find the average age of the tree when the circumference is 30 and when circumference is 214.\n", 866 | "\n", 867 | "\n", 868 | "The dataset is loaded and the variable Orange contains the respective dataset." 869 | ] 870 | }, 871 | { 872 | "cell_type": "code", 873 | "execution_count": 21, 874 | "metadata": {}, 875 | "outputs": [], 876 | "source": [ 877 | "library(datasets)" 878 | ] 879 | }, 880 | { 881 | "cell_type": "code", 882 | "execution_count": 25, 883 | "metadata": {}, 884 | "outputs": [ 885 | { 886 | "data": { 887 | "text/html": [ 888 | "\n", 889 | "\n", 890 | "\n", 891 | "\t\n", 892 | "\t\n", 893 | "\t\n", 894 | "\t\n", 895 | "\t\n", 896 | "\t\n", 897 | "\n", 898 | "
Treeagecircumference
1 118 30
1 484 58
1 664 87
1 1004115
1 1231120
1 1372142
\n" 899 | ], 900 | "text/latex": [ 901 | "\\begin{tabular}{r|lll}\n", 902 | " Tree & age & circumference\\\\\n", 903 | "\\hline\n", 904 | "\t 1 & 118 & 30 \\\\\n", 905 | "\t 1 & 484 & 58 \\\\\n", 906 | "\t 1 & 664 & 87 \\\\\n", 907 | "\t 1 & 1004 & 115 \\\\\n", 908 | "\t 1 & 1231 & 120 \\\\\n", 909 | "\t 1 & 1372 & 142 \\\\\n", 910 | "\\end{tabular}\n" 911 | ], 912 | "text/markdown": [ 913 | "\n", 914 | "| Tree | age | circumference |\n", 915 | "|---|---|---|\n", 916 | "| 1 | 118 | 30 |\n", 917 | "| 1 | 484 | 58 |\n", 918 | "| 1 | 664 | 87 |\n", 919 | "| 1 | 1004 | 115 |\n", 920 | "| 1 | 1231 | 120 |\n", 921 | "| 1 | 1372 | 142 |\n", 922 | "\n" 923 | ], 924 | "text/plain": [ 925 | " Tree age circumference\n", 926 | "1 1 118 30 \n", 927 | "2 1 484 58 \n", 928 | "3 1 664 87 \n", 929 | "4 1 1004 115 \n", 930 | "5 1 1231 120 \n", 931 | "6 1 1372 142 " 932 | ] 933 | }, 934 | "metadata": {}, 935 | "output_type": "display_data" 936 | } 937 | ], 938 | "source": [ 939 | "head(Orange)" 940 | ] 941 | }, 942 | { 943 | "cell_type": "code", 944 | "execution_count": 9, 945 | "metadata": {}, 946 | "outputs": [], 947 | "source": [ 948 | "#### Start solution ####" 949 | ] 950 | }, 951 | { 952 | "cell_type": "code", 953 | "execution_count": null, 954 | "metadata": {}, 955 | "outputs": [], 956 | "source": [] 957 | }, 958 | { 959 | "cell_type": "code", 960 | "execution_count": null, 961 | "metadata": {}, 962 | "outputs": [], 963 | "source": [] 964 | }, 965 | { 966 | "cell_type": "code", 967 | "execution_count": null, 968 | "metadata": {}, 969 | "outputs": [], 970 | "source": [] 971 | }, 972 | { 973 | "cell_type": "code", 974 | "execution_count": null, 975 | "metadata": {}, 976 | "outputs": [], 977 | "source": [] 978 | }, 979 | { 980 | "cell_type": "code", 981 | "execution_count": null, 982 | "metadata": {}, 983 | "outputs": [], 984 | "source": [] 985 | }, 986 | { 987 | "cell_type": "code", 988 | "execution_count": 14, 989 | "metadata": {}, 990 | "outputs": [], 991 | "source": [ 992 | "#### End solution ####" 993 | ] 994 | }, 995 | { 996 | "cell_type": "markdown", 997 | "metadata": {}, 998 | "source": [ 999 | "\n", 1000 | "### Save it and push .ipynb and .html file to your Github Repository\n", 1001 | "## GOOD JOB! You have successfully finished the course!" 1002 | ] 1003 | }, 1004 | { 1005 | "cell_type": "code", 1006 | "execution_count": null, 1007 | "metadata": {}, 1008 | "outputs": [], 1009 | "source": [] 1010 | } 1011 | ], 1012 | "metadata": { 1013 | "kernelspec": { 1014 | "display_name": "Python 3", 1015 | "language": "python", 1016 | "name": "python3" 1017 | }, 1018 | "language_info": { 1019 | "codemirror_mode": { 1020 | "name": "ipython", 1021 | "version": 3 1022 | }, 1023 | "file_extension": ".py", 1024 | "mimetype": "text/x-python", 1025 | "name": "python", 1026 | "nbconvert_exporter": "python", 1027 | "pygments_lexer": "ipython3", 1028 | "version": "3.7.1" 1029 | } 1030 | }, 1031 | "nbformat": 4, 1032 | "nbformat_minor": 4 1033 | } 1034 | --------------------------------------------------------------------------------