You may (in fact, are encouraged to) use the Internet to search up any information to help you with this assignment, though you must cite any external (i.e. non-course related) websites that you use. Similarly, after attempting this assignment by yourself, you may collaborate with other students in the course, but you must each write your own code and acknowledge all students with whom you collaborated for each problem (you don’t need to cite by subpart). However, you may not post on Internet forums (e.g. Stack Exchange) for help with this assignment; doing so is considered an Honor Code violation. You also may not copy verbatim any code from the Internet, even with citation.
Please provide your responses to each problem in this
.Rmd
file in the R code chunks directly below each subpart,
inserting additional R code chunks if needed. For text responses, please
place them between <p>
tags (this places your text in
a grey-background textbox, to make them easier to grade). Please see the
sample after Problem 1(a).
On Gradescope, you need only submit the .html
file
created by knitting the document with your responses. Problem 0 will
provide guidance on how to do this.
Each problem subpart is graded on a 0-1-2 scale, and counts equally within this assignment. Credit is given based on the approach and code, not necessarily the final answer.
Read Sections 2.2-2.6 of “R Markdown: The Definitive Guide”. It will introduce you to using R markdown. You may skip subsection 2.5.3 if you are not familiar with LaTeX. It is not needed for this class.
nycflights13
package.library(nycflights13)
Insert any text responses between <p>
HTML tags, with
class=“comment”. This is an example of how to do this; you may want to
copy this line of text and the tags in the line above and the line
below, and then paste at every point in the document where you want to
insert text. Finally, replace this text with the words you want to
write.
airports
tibble (data frame). How many
observations and variables does it have?nrow(airports)
## [1] 1458
ncol(airports)
## [1] 8
Recalling that each row in a tibble corresponds to an observation and
each column to a variable, we conclude that airports
has
1,458 observations and 8 variables.
We start by looking at the help page for the tibble
airports
:
?airports
This shows us that the column alt
refers to altitude in
feet. We want to take the mean value of all numbers in this column. We
can extract this column via indexing, and then use the built-in
mean()
function from Lecture 2:
mean(airports$alt)
## [1] 1001.416
The mean altitude is 1001.416 feet.
unique()
.
The help page for unique()
tells us that this function
gives us the unique values in a vector. From the help page for the
airports
tibble we know the time zones are given in the
column labeled tzone
. Thus the following code stores the
unique time zones in the tibble:
unique_tzones <- unique(airports$tzone)
To get the number of unique time zones, we could print out our variable
unique_tzones
, and count the number of entries.
Alternatively (better), we could do this programatically using the
length()
function:
length(unique_tzones)
## [1] 10
airports[c(3,5), c("name", "lat", "lon", "alt")]
## # A tibble: 2 × 4
## name lat lon alt
## <chr> <dbl> <dbl> <dbl>
## 1 Schaumburg Regional 42.0 -88.1 801
## 2 Jekyll Island Airport 31.1 -81.4 11
percent_decrease
that takes in
two arguments — num
and percent
— and returns
the value when num
is decreased by percent
percent. For instance, if num
is 10 and
percent
is 20, your function should return 8.percent_decrease <- function(num, percent){
return(num*(1-percent/100))
}
round()
to print out the price to the nearest
cent.
The help menu for round()
tells us that the second argument
specifies how many digits after the decimal point to round to. A cent
has two decimal places, so our second argument needs to be 2:
round(percent_decrease(num=39.95, percent=40), 2)
## [1] 23.97
evens
containing all even integers
between 0 and 2022, inclusive. Hint: look at the help menu for the
seq()
function!evens <- seq(from=0, to=2022, by=2)
decreased_evens <- percent_decrease(num=evens, percent=50)
Note that evens
is a numeric vector, even though you
designed your function percent_decrease()
to take in a
single number as its first argument. Explain how R makes sense of this.
Hint: This is loosely related to the concepts of “broadcasting” and
“vectorization”.
We look at what’s inside decreased_evens
:
decreased_evens
## [1] 0 1 2 3 4 5 6 7 8 9 10 11 12 13
## [15] 14 15 16 17 18 19 20 21 22 23 24 25 26 27
## [29] 28 29 30 31 32 33 34 35 36 37 38 39 40 41
## [43] 42 43 44 45 46 47 48 49 50 51 52 53 54 55
## [57] 56 57 58 59 60 61 62 63 64 65 66 67 68 69
## [71] 70 71 72 73 74 75 76 77 78 79 80 81 82 83
## [85] 84 85 86 87 88 89 90 91 92 93 94 95 96 97
## [99] 98 99 100 101 102 103 104 105 106 107 108 109 110 111
## [113] 112 113 114 115 116 117 118 119 120 121 122 123 124 125
## [127] 126 127 128 129 130 131 132 133 134 135 136 137 138 139
## [141] 140 141 142 143 144 145 146 147 148 149 150 151 152 153
## [155] 154 155 156 157 158 159 160 161 162 163 164 165 166 167
## [169] 168 169 170 171 172 173 174 175 176 177 178 179 180 181
## [183] 182 183 184 185 186 187 188 189 190 191 192 193 194 195
## [197] 196 197 198 199 200 201 202 203 204 205 206 207 208 209
## [211] 210 211 212 213 214 215 216 217 218 219 220 221 222 223
## [225] 224 225 226 227 228 229 230 231 232 233 234 235 236 237
## [239] 238 239 240 241 242 243 244 245 246 247 248 249 250 251
## [253] 252 253 254 255 256 257 258 259 260 261 262 263 264 265
## [267] 266 267 268 269 270 271 272 273 274 275 276 277 278 279
## [281] 280 281 282 283 284 285 286 287 288 289 290 291 292 293
## [295] 294 295 296 297 298 299 300 301 302 303 304 305 306 307
## [309] 308 309 310 311 312 313 314 315 316 317 318 319 320 321
## [323] 322 323 324 325 326 327 328 329 330 331 332 333 334 335
## [337] 336 337 338 339 340 341 342 343 344 345 346 347 348 349
## [351] 350 351 352 353 354 355 356 357 358 359 360 361 362 363
## [365] 364 365 366 367 368 369 370 371 372 373 374 375 376 377
## [379] 378 379 380 381 382 383 384 385 386 387 388 389 390 391
## [393] 392 393 394 395 396 397 398 399 400 401 402 403 404 405
## [407] 406 407 408 409 410 411 412 413 414 415 416 417 418 419
## [421] 420 421 422 423 424 425 426 427 428 429 430 431 432 433
## [435] 434 435 436 437 438 439 440 441 442 443 444 445 446 447
## [449] 448 449 450 451 452 453 454 455 456 457 458 459 460 461
## [463] 462 463 464 465 466 467 468 469 470 471 472 473 474 475
## [477] 476 477 478 479 480 481 482 483 484 485 486 487 488 489
## [491] 490 491 492 493 494 495 496 497 498 499 500 501 502 503
## [505] 504 505 506 507 508 509 510 511 512 513 514 515 516 517
## [519] 518 519 520 521 522 523 524 525 526 527 528 529 530 531
## [533] 532 533 534 535 536 537 538 539 540 541 542 543 544 545
## [547] 546 547 548 549 550 551 552 553 554 555 556 557 558 559
## [561] 560 561 562 563 564 565 566 567 568 569 570 571 572 573
## [575] 574 575 576 577 578 579 580 581 582 583 584 585 586 587
## [589] 588 589 590 591 592 593 594 595 596 597 598 599 600 601
## [603] 602 603 604 605 606 607 608 609 610 611 612 613 614 615
## [617] 616 617 618 619 620 621 622 623 624 625 626 627 628 629
## [631] 630 631 632 633 634 635 636 637 638 639 640 641 642 643
## [645] 644 645 646 647 648 649 650 651 652 653 654 655 656 657
## [659] 658 659 660 661 662 663 664 665 666 667 668 669 670 671
## [673] 672 673 674 675 676 677 678 679 680 681 682 683 684 685
## [687] 686 687 688 689 690 691 692 693 694 695 696 697 698 699
## [701] 700 701 702 703 704 705 706 707 708 709 710 711 712 713
## [715] 714 715 716 717 718 719 720 721 722 723 724 725 726 727
## [729] 728 729 730 731 732 733 734 735 736 737 738 739 740 741
## [743] 742 743 744 745 746 747 748 749 750 751 752 753 754 755
## [757] 756 757 758 759 760 761 762 763 764 765 766 767 768 769
## [771] 770 771 772 773 774 775 776 777 778 779 780 781 782 783
## [785] 784 785 786 787 788 789 790 791 792 793 794 795 796 797
## [799] 798 799 800 801 802 803 804 805 806 807 808 809 810 811
## [813] 812 813 814 815 816 817 818 819 820 821 822 823 824 825
## [827] 826 827 828 829 830 831 832 833 834 835 836 837 838 839
## [841] 840 841 842 843 844 845 846 847 848 849 850 851 852 853
## [855] 854 855 856 857 858 859 860 861 862 863 864 865 866 867
## [869] 868 869 870 871 872 873 874 875 876 877 878 879 880 881
## [883] 882 883 884 885 886 887 888 889 890 891 892 893 894 895
## [897] 896 897 898 899 900 901 902 903 904 905 906 907 908 909
## [911] 910 911 912 913 914 915 916 917 918 919 920 921 922 923
## [925] 924 925 926 927 928 929 930 931 932 933 934 935 936 937
## [939] 938 939 940 941 942 943 944 945 946 947 948 949 950 951
## [953] 952 953 954 955 956 957 958 959 960 961 962 963 964 965
## [967] 966 967 968 969 970 971 972 973 974 975 976 977 978 979
## [981] 980 981 982 983 984 985 986 987 988 989 990 991 992 993
## [995] 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
## [1009] 1008 1009 1010 1011
str(decreased_evens)
## num [1:1012] 0 1 2 3 4 5 6 7 8 9 ...
We observe that decreased_evens
is a vector with the same
length as evens; R has decreased each entry in evens
by
50%.
decreased_evens_2 <- percent_decrease(num=evens, percent=c(50,100))
Explain what’s going on now.
We look at the values again:
decreased_evens_2
## [1] 0 0 2 0 4 0 6 0 8 0 10 0 12 0
## [15] 14 0 16 0 18 0 20 0 22 0 24 0 26 0
## [29] 28 0 30 0 32 0 34 0 36 0 38 0 40 0
## [43] 42 0 44 0 46 0 48 0 50 0 52 0 54 0
## [57] 56 0 58 0 60 0 62 0 64 0 66 0 68 0
## [71] 70 0 72 0 74 0 76 0 78 0 80 0 82 0
## [85] 84 0 86 0 88 0 90 0 92 0 94 0 96 0
## [99] 98 0 100 0 102 0 104 0 106 0 108 0 110 0
## [113] 112 0 114 0 116 0 118 0 120 0 122 0 124 0
## [127] 126 0 128 0 130 0 132 0 134 0 136 0 138 0
## [141] 140 0 142 0 144 0 146 0 148 0 150 0 152 0
## [155] 154 0 156 0 158 0 160 0 162 0 164 0 166 0
## [169] 168 0 170 0 172 0 174 0 176 0 178 0 180 0
## [183] 182 0 184 0 186 0 188 0 190 0 192 0 194 0
## [197] 196 0 198 0 200 0 202 0 204 0 206 0 208 0
## [211] 210 0 212 0 214 0 216 0 218 0 220 0 222 0
## [225] 224 0 226 0 228 0 230 0 232 0 234 0 236 0
## [239] 238 0 240 0 242 0 244 0 246 0 248 0 250 0
## [253] 252 0 254 0 256 0 258 0 260 0 262 0 264 0
## [267] 266 0 268 0 270 0 272 0 274 0 276 0 278 0
## [281] 280 0 282 0 284 0 286 0 288 0 290 0 292 0
## [295] 294 0 296 0 298 0 300 0 302 0 304 0 306 0
## [309] 308 0 310 0 312 0 314 0 316 0 318 0 320 0
## [323] 322 0 324 0 326 0 328 0 330 0 332 0 334 0
## [337] 336 0 338 0 340 0 342 0 344 0 346 0 348 0
## [351] 350 0 352 0 354 0 356 0 358 0 360 0 362 0
## [365] 364 0 366 0 368 0 370 0 372 0 374 0 376 0
## [379] 378 0 380 0 382 0 384 0 386 0 388 0 390 0
## [393] 392 0 394 0 396 0 398 0 400 0 402 0 404 0
## [407] 406 0 408 0 410 0 412 0 414 0 416 0 418 0
## [421] 420 0 422 0 424 0 426 0 428 0 430 0 432 0
## [435] 434 0 436 0 438 0 440 0 442 0 444 0 446 0
## [449] 448 0 450 0 452 0 454 0 456 0 458 0 460 0
## [463] 462 0 464 0 466 0 468 0 470 0 472 0 474 0
## [477] 476 0 478 0 480 0 482 0 484 0 486 0 488 0
## [491] 490 0 492 0 494 0 496 0 498 0 500 0 502 0
## [505] 504 0 506 0 508 0 510 0 512 0 514 0 516 0
## [519] 518 0 520 0 522 0 524 0 526 0 528 0 530 0
## [533] 532 0 534 0 536 0 538 0 540 0 542 0 544 0
## [547] 546 0 548 0 550 0 552 0 554 0 556 0 558 0
## [561] 560 0 562 0 564 0 566 0 568 0 570 0 572 0
## [575] 574 0 576 0 578 0 580 0 582 0 584 0 586 0
## [589] 588 0 590 0 592 0 594 0 596 0 598 0 600 0
## [603] 602 0 604 0 606 0 608 0 610 0 612 0 614 0
## [617] 616 0 618 0 620 0 622 0 624 0 626 0 628 0
## [631] 630 0 632 0 634 0 636 0 638 0 640 0 642 0
## [645] 644 0 646 0 648 0 650 0 652 0 654 0 656 0
## [659] 658 0 660 0 662 0 664 0 666 0 668 0 670 0
## [673] 672 0 674 0 676 0 678 0 680 0 682 0 684 0
## [687] 686 0 688 0 690 0 692 0 694 0 696 0 698 0
## [701] 700 0 702 0 704 0 706 0 708 0 710 0 712 0
## [715] 714 0 716 0 718 0 720 0 722 0 724 0 726 0
## [729] 728 0 730 0 732 0 734 0 736 0 738 0 740 0
## [743] 742 0 744 0 746 0 748 0 750 0 752 0 754 0
## [757] 756 0 758 0 760 0 762 0 764 0 766 0 768 0
## [771] 770 0 772 0 774 0 776 0 778 0 780 0 782 0
## [785] 784 0 786 0 788 0 790 0 792 0 794 0 796 0
## [799] 798 0 800 0 802 0 804 0 806 0 808 0 810 0
## [813] 812 0 814 0 816 0 818 0 820 0 822 0 824 0
## [827] 826 0 828 0 830 0 832 0 834 0 836 0 838 0
## [841] 840 0 842 0 844 0 846 0 848 0 850 0 852 0
## [855] 854 0 856 0 858 0 860 0 862 0 864 0 866 0
## [869] 868 0 870 0 872 0 874 0 876 0 878 0 880 0
## [883] 882 0 884 0 886 0 888 0 890 0 892 0 894 0
## [897] 896 0 898 0 900 0 902 0 904 0 906 0 908 0
## [911] 910 0 912 0 914 0 916 0 918 0 920 0 922 0
## [925] 924 0 926 0 928 0 930 0 932 0 934 0 936 0
## [939] 938 0 940 0 942 0 944 0 946 0 948 0 950 0
## [953] 952 0 954 0 956 0 958 0 960 0 962 0 964 0
## [967] 966 0 968 0 970 0 972 0 974 0 976 0 978 0
## [981] 980 0 982 0 984 0 986 0 988 0 990 0 992 0
## [995] 994 0 996 0 998 0 1000 0 1002 0 1004 0 1006 0
## [1009] 1008 0 1010 0
str(decreased_evens_2)
## num [1:1012] 0 0 2 0 4 0 6 0 8 0 ...
The first entry of evens
was decreased by 50%. The second
entry was decreased by 100% (i.e. set to 0). Then the third entry was
decreased by 50%, the fourth by 100%, etc.
Karen needs to buy cookies for her daughter’s 6th birthday party. She only shops at Costco, which sells cookies in batches of 12. There will be 21 cookie-eating guests at the party.
leftovers
specifying how many left over cookies she will have for each number of
batches from 2 to 10. Hint: the seq()
function from problem
2 may come in handy, as well as the modulo operator.leftovers <- (12*seq(from=2, to=10, by=1)) %% 21
leftovers
to a named list with character names
“2”,…,“10”, corresponding to the number of batches.leftovers <- as.list(leftovers)
names(leftovers) <- as.character(seq(from=2, to=10, by=1))
leftovers < 4
## 2 3 4 5 6 7 8 9 10
## TRUE FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE
leftovers[leftovers < 4]
## $`2`
## [1] 3
##
## $`7`
## [1] 0
##
## $`9`
## [1] 3
We use str()
to figure out the structure of the output of
each line of code.
str(leftovers < 4)
## Named logi [1:9] TRUE FALSE FALSE FALSE FALSE TRUE ...
## - attr(*, "names")= chr [1:9] "2" "3" "4" "5" ...
str(leftovers[leftovers < 4])
## List of 3
## $ 2: num 3
## $ 7: num 0
## $ 9: num 3
The first line returns a logical vector of length 9. Each entry of this
logical vector is TRUE if the corresponding entry of
leftovers
is less than 4, and FALSE otherwise. The second
line returns a (named) list consisting of all entries in
leftovers
that are less than 4.
names()
and part (c) to return all numbers of
batches Karen can purchase to have less than 4 cookies left over.names(leftovers[leftovers<4])
## [1] "2" "7" "9"
leftovers[-2]
## $`2`
## [1] 3
##
## $`4`
## [1] 6
##
## $`5`
## [1] 18
##
## $`6`
## [1] 9
##
## $`7`
## [1] 0
##
## $`8`
## [1] 12
##
## $`9`
## [1] 3
##
## $`10`
## [1] 15
str(leftovers[-2])
## List of 8
## $ 2 : num 3
## $ 4 : num 6
## $ 5 : num 18
## $ 6 : num 9
## $ 7 : num 0
## $ 8 : num 12
## $ 9 : num 3
## $ 10: num 15
We observe this code extracts all but the 2nd entry of
leftovers
. The result is a (named) list of length 8.