A loop is simply a type of algorithm, in this case, a procedure in which we direct R to iteratively check for a particular condition and, depending on the state of that condition, conduct a particular action or terminate the algorithm.
Imagine we are conducting a randomized controlled trial and we want to assign study participants randomly to either the treatment or control arm. We decide we’ll flip a coin for each participant and assign them to the treatment group if the coin comes up heads and to the control group if it comes up tails. Assuming we have 100 participants to assign, the following text provides an informal description of a loop designed to carry out this task:
Here’s the equivalent in code, where our coin flip is a random draw of 1 or 0:
# generate participant IDs
1:100
pid <-
# set up a vector to store treatment group assignment
vector()
rx <-
# set random seed so we get same set of coin flips each time
set.seed(98765)
for (i in 1:100) {
rbinom(1, 1, prob = 0.5)
coinflip <-
if (coinflip == 1) {
"treatment"
rx[i] <-else {
} "control"
rx[i] <-
}
}
data.frame(pid, rx)
pid rx
1 1 treatment
2 2 control
3 3 control
4 4 treatment
5 5 control
6 6 treatment
7 7 control
8 8 control
9 9 treatment
10 10 treatment
11 11 treatment
12 12 treatment
13 13 control
14 14 treatment
15 15 control
16 16 treatment
17 17 control
18 18 control
19 19 control
20 20 control
21 21 control
22 22 treatment
23 23 control
24 24 treatment
25 25 control
26 26 control
27 27 treatment
28 28 control
29 29 treatment
30 30 treatment
31 31 control
32 32 control
33 33 control
34 34 treatment
35 35 treatment
36 36 treatment
37 37 control
38 38 treatment
39 39 control
40 40 treatment
41 41 control
42 42 treatment
43 43 treatment
44 44 control
45 45 treatment
46 46 treatment
47 47 treatment
48 48 control
49 49 control
50 50 control
51 51 control
52 52 treatment
53 53 treatment
54 54 control
55 55 treatment
56 56 control
57 57 control
58 58 treatment
59 59 treatment
60 60 treatment
61 61 control
62 62 treatment
63 63 control
64 64 treatment
65 65 control
66 66 treatment
67 67 control
68 68 treatment
69 69 treatment
70 70 treatment
71 71 treatment
72 72 treatment
73 73 control
74 74 treatment
75 75 control
76 76 treatment
77 77 treatment
78 78 treatment
79 79 control
80 80 control
81 81 control
82 82 treatment
83 83 control
84 84 control
85 85 control
86 86 control
87 87 treatment
88 88 treatment
89 89 control
90 90 control
91 91 treatment
92 92 treatment
93 93 control
94 94 treatment
95 95 control
96 96 treatment
97 97 control
98 98 treatment
99 99 treatment
100 100 treatment
The code above demonstrates the use of a for-loop. We also could have used a while-loop, which would have started as while (i <= 100)
.
We can also express our algorithm as a flowchart. Often, mapping algorithms visually can help us as we plan or design loops and statistical programs in general. Furthermore, they make communicating complex procedures much easier.
Loops are important and powerful tools in statistical programming and, when combined with logical operations, provide you with a means to develop sophisticated, flexible code bases.
In this chapter, we’ll focus on some simple implementations of loops, focusing separately on self-written loops like the one displayed above and vectorized loops in R. We’ll also take a look at a couple of looping procedures in SAS.
Looping often arises as a practical solution to repeat some task on a series of objects.
Let’s say we had a list of numbers and wanted to multiple each of them by 10. Pretending we don’t know about R’s recycling capabilities, we might write a loop to conduct this task.
# generate a list of arbitrary numbers to operate upon
seq(from = 10, to = 100, by = 10)
numlist <-
# first, we need to create an empty object in which to store our results
vector()
results <-
# loop
for (i in 1:length(numlist)) {
numlist[i] * 10
results[i] <-
}
print(results)
[1] 100 200 300 400 500 600 700 800 900 1000
To gain a little intuition, run the following loops and observe the results: for (i in 1:3) print(i)
, for (i in 32:60) print(i)
.
The i
referred to in the for
statement is a variable R creates to keep track of the current iteration of the loop. For instance, if R is processing the first iteration, i
is stored as the number we’ve initialized after the in
statement—in this case, we initialized the loop at 1. When R is on the third iteration, i
is stored in the background as 3.
Therefore, based on what we’ve already learned in prior chapters, we can break down the steps as follows:
Create a sequence of numbers increasing from 10 to 100 by 10, and store it to the object numlist
. (What kind of object is numlist
?)
Create the empty vector results
.
For each iteration i
in the range 1–10, do the following:
numlist
by 10 and store the result in the ith element of results
.Print the vector of results.
Note that we don’t always have to use curly braces in our loops. Because our loop above is quite short, we could have written it as:
for (i in 1:length(numlist)) results[i] <- numlist[i] * 10
However, the syntax above requires that all the code be written in a single line. Moving forward, we’ll almost always use curly braces when using procedures that allow them. These include not only for
loops but if
/else
statements and function
definitions.
Usually, we’ll be using loops to conduct more complicated operations. Let’s pretend R doesn’t have all these nifty built-in tools to summarize our data or create new variables.
Imagine instead that we’re tasked with developing summary statistics for both categorical and continuous variables. To do so, we’ll have to do the following:
I’ve selected a handful of variables from the NHEFS dataset we’ve already worked with.
head(nh)
# A tibble: 6 × 8
seqn death sex age race smokeyrs smokeintensity marital
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 233 0 0 42 1 29 30 2
2 235 0 0 36 0 24 20 2
3 244 0 1 56 1 26 20 3
4 245 1 0 68 1 53 3 3
5 252 0 0 40 0 19 20 2
6 257 0 1 43 1 21 10 4
However, instead of storing the data in a data frame, I’ve stored it in a list for the purposes of the current examples.
head(nhl)
$seqn
[1] 233 235 244 245 252 257 262 266 419 420 428 431
[13] 434 443 446 455 457 596 603 604 605 616 618 619
[25] 620 804 806 813 816 818 825 828 831 1094 1096 1101
[37] 1103 1104 1106 1109 1116 1120 1124 1126 1127 1129 1133 1135
[49] 1148 1476 1480 1498 1505 1513 1515 1519 1523 1535 1538 1539
[61] 1542 1753 1762 1769 1776 1778 1781 1789 1793 1936 1938 1939
[73] 1941 1943 1945 1951 1964 1967 1968 2246 2248 2249 2251 2254
[85] 2255 2259 2260 2263 2267 2273 2279 2281 2284 2285 2286 2287
[97] 2289 2290 2293 2295 2505 2507 2508 2510 2511 2518 2522 2524
[109] 2525 2528 2530 2694 2700 2704 2713 2716 2721 2722 2937 2940
[121] 2949 2955 3110 3119 3121 3128 3134 3322 3332 3337 3338 3353
[133] 3356 3357 3682 3683 3684 3687 3696 3701 3702 3706 3709 3713
[145] 3717 3721 3724 3731 3735 3926 3928 3934 3944 3947 3948 4234
[157] 4236 4237 4238 4244 4245 4251 4257 4258 4270 4278 4281 4286
[169] 4287 4289 4528 4544 4545 4546 4548 4556 4559 4561 4562 4566
[181] 4567 4944 4946 4958 4959 4966 4970 4971 4973 4976 4979 4986
[193] 4987 4988 4990 4994 4999 5011 5014 5015 5403 5404 5411 5412
[205] 5415 5422 5423 5431 5432 5433 5443 5454 5464 5469 5477 5478
[217] 5480 5490 5497 5739 5743 5744 5748 5761 5774 6210 6217 6220
[229] 6222 6227 6237 6240 6249 6254 6268 6270 6272 6273 6299 6309
[241] 6313 6532 6534 6537 6538 6544 6546 6549 6551 6556 6560 6561
[253] 6566 6569 6917 6918 6924 6926 6927 6928 6932 6933 6944 6952
[265] 6953 6960 6977 6985 6986 6994 6997 7306 7310 7312 7315 7326
[277] 7335 7337 7345 7346 7354 7359 7629 7633 7636 7645 7646 7648
[289] 7651 7653 7662 7667 7670 7677 7682 7891 7892 7900 7902 7908
[301] 7912 7922 7924 7927 7932 7939 7941 7942 8128 8130 8134 8135
[313] 8137 8142 8151 8165 8166 8368 8370 8379 8388 8395 8402 8403
[325] 8406 8412 8620 8623 8626 8627 8633 8637 8638 8644 8649 8655
[337] 8656 8657 8658 8660 9007 9009 9011 9012 9024 9033 9051 9054
[349] 9059 9287 9292 9295 9308 9315 9320 9323 9326 9327 9331 9334
[361] 9336 9339 9559 9561 9562 9573 9574 9579 9582 9585 9587 9591
[373] 9600 9608 9609 9846 9854 9857 9870 9878 9883 9884 9887 9892
[385] 10097 10099 10102 10104 10106 10108 10110 10112 10113 10122 10367 10370
[397] 10371 10376 10380 10393 10395 10399 10402 10403 10600 10605 10606 10607
[409] 10610 10614 10618 10619 10620 10625 10626 10627 10634 10635 10636 10970
[421] 10981 10990 10992 10995 11002 11004 11010 11011 11325 11329 11330 11336
[433] 11337 11338 11340 11348 11349 11351 11355 11356 11363 11366 11368 11376
[445] 11377 11380 11383 11384 11385 11828 11836 11845 11850 11855 11864 11866
[457] 11876 11877 11880 11882 11886 11892 11894 11900 11908 12262 12263 12266
[469] 12268 12269 12271 12273 12275 12277 12279 12284 12285 12290 12299 12305
[481] 12312 12314 12318 12321 12627 12634 12639 12640 12644 12648 12650 12651
[493] 12654 12667 12673 12675 12684 12685 12686 12690 12926 12927 12929 12944
[505] 12948 12954 12958 13203 13206 13211 13214 13231 13235 13236 13250 13252
[517] 13254 13255 13558 13562 13567 13572 13575 13576 13578 13581 13593 13595
[529] 13602 13604 13610 13612 13619 13627 13631 13632 13634 13635 13636 14009
[541] 14023 14025 14028 14032 14037 14038 14039 14040 14041 14042 14047 14051
[553] 14055 14056 14059 14066 14067 14068 14074 14076 14077 14085 14086 14088
[565] 14092 14093 14096 14101 14442 14444 14456 14460 14461 14469 14470 14478
[577] 14479 14480 14486 14487 14488 14494 14496 14515 14516 14967 14969 14970
[589] 14971 14976 14982 14983 14985 14986 14987 14993 14997 15000 15002 15005
[601] 15010 15011 15017 15024 15026 15028 15031 15039 15264 15267 15272 15273
[613] 15274 15276 15277 15281 15283 15284 15289 15290 15300 15600 15605 15611
[625] 15617 15619 15624 15625 15628 15630 15632 15633 15639 15647 15650 15652
[637] 15653 15655 15839 15842 15844 15852 15853 15859 15865 15874 16295 16303
[649] 16304 16306 16307 16317 16322 16333 16354 16363 16366 16369 16377 16380
[661] 16382 16393 16395 16696 16697 16698 16707 16709 16715 16717 16725 16729
[673] 16731 16742 16747 16748 16749 17040 17041 17044 17050 17051 17052 17053
[685] 17055 17056 17068 17081 17083 17084 17086 17087 17089 17093 17096 17374
[697] 17375 17376 17380 17385 17393 17399 17401 17407 17408 17410 17414 17416
[709] 17419 17420 17421 17422 17424 17425 17734 17738 17744 17755 17756 17757
[721] 17760 17768 17770 17776 17777 17779 17785 17786 18054 18059 18062 18067
[733] 18069 18073 18084 18085 18088 18091 18093 18097 18101 18102 18103 18107
[745] 18108 18109 18314 18315 18318 18319 18320 18322 18325 18340 18348 18757
[757] 18762 18763 18766 18772 18779 18783 18793 18804 18810 18822 18825 18826
[769] 18849 18867 19117 19132 19146 19147 19153 19154 19163 19165 19570 19576
[781] 19588 19605 19611 19616 19622 19633 19634 19638 19639 19641 19863 19867
[793] 19877 19881 19885 19894 20047 20049 20054 20055 20057 20063 20067 20068
[805] 20299 20303 20304 20306 20308 20315 20317 20318 20321 20326 20333 20336
[817] 20337 20345 20346 20347 20681 20693 20699 20701 20702 20703 20706 20707
[829] 20711 20715 20718 20723 20724 20727 20730 20740 20742 20745 20751 20755
[841] 20756 20760 20768 20771 20781 20783 20784 20791 20794 20826 20834 20837
[853] 20843 20848 20853 20857 20859 20868 20869 20872 20874 20876 20883 20885
[865] 20888 20891 20894 20899 20900 20901 20907 20908 20909 20913 20914 20915
[877] 20923 20927 20938 20940 20943 20945 20947 20951 20959 20960 20961 20963
[889] 20964 20966 20969 20972 20973 20976 20983 20984 20990 20995 21008 21009
[901] 21011 21012 21017 21019 21025 21029 21030 21035 21038 21042 21044 21057
[913] 21058 21069 21070 21083 21094 21101 21102 21103 21107 21108 21109 21124
[925] 21128 21130 21134 21139 21140 21151 21157 21166 21170 21173 21177 21185
[937] 21189 21215 21219 21221 21224 21227 21231 21233 21241 21244 21247 21255
[949] 21259 21260 21261 21262 21265 21266 21271 21275 21276 21280 21281 21291
[961] 21296 21305 21307 21309 21311 21323 21325 21329 21331 21332 21339 21350
[973] 21351 21354 21365 21366 21369 21371 21380 21382 21388 21391 21392 21396
[985] 21398 21401 21415 21420 21428 21429 21433 21463 21468 21469 21475 21490
[997] 21494 21495 21497 21501 21513 21515 21522 21537 21542 21550 21561 21578
[1009] 21580 21581 21585 21589 21597 21602 21608 21610 21621 21623 21626 21643
[1021] 21692 21697 21707 21718 21730 21731 21733 21734 21735 21740 21751 21753
[1033] 21754 21760 21763 21765 21772 21777 21782 21785 21792 21795 21796 21797
[1045] 21798 21799 21800 21801 21803 21819 21823 21826 21829 21830 21832 21839
[1057] 21849 21851 21861 21862 21863 21865 21870 21875 21882 21884 21897 21901
[1069] 21908 21909 21910 21913 21914 21921 21923 21925 21929 21931 21933 21936
[1081] 21940 21951 21960 21965 21966 21973 21982 21983 22005 22007 22011 22017
[1093] 22023 22025 22034 22035 22038 22044 22046 22047 22065 22071 22075 22083
[1105] 22089 22129 22130 22131 22133 22139 22146 22148 22155 22162 22164 22165
[1117] 22168 22169 22170 22173 22174 22183 22214 22215 22216 22224 22244 22245
[1129] 22271 22272 22274 22277 22283 22292 22294 22300 22306 22307 22318 22321
[1141] 22325 22331 22336 22341 22342 22345 22374 22383 22394 22396 22400 22401
[1153] 22402 22408 22412 22413 22415 22419 22428 22431 22437 22446 22450 22451
[1165] 22456 22458 22481 22497 22508 22516 22519 22520 22521 22522 22524 22528
[1177] 22530 22533 22535 22536 22544 22551 22554 22567 22568 22582 22583 22585
[1189] 22586 22588 22592 22604 22606 22608 22615 22625 22632 22635 22641 22642
[1201] 22643 22644 22658 22659 22662 22668 22673 22674 22677 22678 22681 22685
[1213] 22686 22688 22694 22695 22698 22701 22705 22709 22718 22719 22720 22734
[1225] 22740 22759 22761 22764 22773 22778 22782 22788 22792 22796 22805 22818
[1237] 22886 22888 22890 22892 22893 22895 22903 22904 22908 22910 22914 22915
[1249] 22927 22930 22933 22940 22941 22946 22953 22955 22957 22979 22980 22984
[1261] 23012 23015 23016 23020 23021 23022 23023 23030 23031 23038 23039 23041
[1273] 23044 23045 23047 23048 23052 23053 23054 23055 23057 23064 23065 23071
[1285] 23075 23079 23083 23084 23086 23087 23088 23099 23105 23118 23121 23122
[1297] 23140 23141 23142 23155 23158 23166 23169 23180 23193 23201 23206 23214
[1309] 23224 23235 23238 23255 23258 23261 23264 23269 23274 23278 23285 23289
[1321] 23292 23296 23298 23299 23300 23303 23310 23311 23321 23362 23375 23376
[1333] 23378 23379 23382 23383 23385 23390 23392 23393 23398 23406 23407 23411
[1345] 23413 23415 23416 23417 23420 23422 23425 23433 23445 23456 23462 23465
[1357] 23486 23493 23495 23497 23502 23508 23514 23519 23520 23521 23522 23525
[1369] 23533 23535 23539 23541 23546 23548 23549 23550 23566 23572 23575 23577
[1381] 23589 23590 23591 23595 23607 23617 23621 23626 23627 23628 23630 23648
[1393] 23651 23662 23666 23675 23679 23686 23696 23707 23716 23727 23729 23733
[1405] 23740 23753 23758 23773 23774 23779 23800 23803 23804 23807 23814 23823
[1417] 23826 23827 23828 23829 23830 23843 23846 23856 23866 23873 23875 23887
[1429] 23897 23900 23923 23924 23938 23940 23947 23962 23964 23965 23973 23983
[1441] 23985 23990 24005 24006 24007 24009 24011 24013 24021 24027 24032 24034
[1453] 24035 24037 24039 24049 24052 24053 24058 24059 24061 24065 24067 24077
[1465] 24078 24088 24091 24114 24118 24122 24129 24134 24135 24140 24151 24162
[1477] 24164 24168 24171 24173 24178 24180 24188 24189 24198 24203 24209 24214
[1489] 24215 24225 24226 24233 24239 24247 24248 24254 24262 24272 24275 24280
[1501] 24300 24305 24306 24313 24315 24319 24334 24336 24345 24347 24354 24355
[1513] 24358 24362 24363 24364 24365 24374 24378 24379 24380 24384 24387 24391
[1525] 24396 24397 24403 24423 24428 24430 24440 24443 24446 24454 24457 24470
[1537] 24499 24500 24501 24502 24503 24533 24546 24552 24559 24563 24577 24578
[1549] 24580 24583 24584 24585 24589 24591 24596 24597 24600 24619 24638 24649
[1561] 24652 24668 24679 24690 24698 24705 24707 24710 24713 24719 24721 24722
[1573] 24723 24724 24725 24726 24729 24735 24737 24741 24746 24770 24810 24817
[1585] 24820 24823 24825 24828 24830 24834 24837 24842 24844 24845 24847 24852
[1597] 24859 24860 24878 24895 24896 24927 24936 24943 24948 24949 24952 24953
[1609] 24959 24963 24964 24968 24969 24970 24971 24974 24983 24986 24995 24998
[1621] 25001 25005 25010 25013 25014 25016 25024 25032 25061
$death
[1] 0 0 0 1 0 0 0 0 1 1 0 1 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 1 0 0 1 0 0
[38] 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 1 0 1 0 1 0 0 0 0 0 0 0
[75] 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1
[112] 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
[149] 0 1 0 1 1 0 0 0 1 1 1 0 0 0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 1 1 0 0 0 0 1 0 0 0 1 0 0
[1444] 0 0 0 0 0 1 0 0 0 1 1 1 0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 0 1 1 1 0 1 1 1 1 0
[1481] 1 1 1 0 1 1 0 0 0 1 1 0 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 0 1 0 0 0 1 1 1 0
[1518] 1 1 0 0 1 1 0 1 1 0 0 0 0 1 1 1 0 1 0 0 1 1 0 1 0 0 0 1 1 1 1 1 1 1 1 1 1
[1555] 1 1 1 1 1 1 0 0 1 0 0 0 0 1 1 0 0 1 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 1 0 1 0
[1592] 1 0 1 1 0 1 1 1 1 0 1 1 0 1 1 0 0 0 0 1 1 1 0 1 0 1 1 0 1 1 1 0 1 0 1 1 0
[1629] 0
$age
[1] 42 36 56 68 40 43 56 29 51 43 43 34 54 51 71 47 56 48 47 47 25 38 56 28
[25] 60 27 47 53 33 41 52 57 35 41 72 33 34 35 28 26 64 35 63 45 40 69 33 45
[49] 37 49 51 28 36 45 47 57 57 35 50 56 55 27 64 25 32 34 36 34 40 47 54 35
[73] 48 33 70 67 32 37 65 26 51 33 34 53 56 37 45 55 59 36 35 39 30 32 45 55
[97] 49 30 51 49 54 52 43 35 27 29 42 39 50 54 65 27 46 32 43 49 70 54 58 32
[121] 50 37 26 66 25 45 40 38 43 56 58 25 46 45 42 30 54 28 29 45 43 47 28 30
[145] 35 43 48 42 54 61 36 34 70 53 53 62 67 60 63 29 58 50 27 63 34 33 59 34
[169] 61 43 38 40 55 34 56 54 59 52 25 65 63 25 29 52 44 30 71 63 40 54 61 32
[193] 58 42 30 54 56 30 62 47 56 35 56 53 32 54 55 69 36 51 49 58 39 55 49 25
[217] 45 50 38 34 32 32 59 68 47 35 57 54 46 26 39 53 61 49 54 41 44 25 60 49
[241] 44 38 61 60 34 41 60 40 38 49 46 48 32 48 49 60 59 62 51 42 38 62 56 50
[265] 44 38 39 25 26 65 58 44 46 44 57 51 29 53 58 65 53 30 41 47 35 56 54 58
[289] 57 34 54 54 35 64 55 45 53 64 28 64 67 33 39 52 44 46 69 57 47 25 44 46
[313] 25 37 28 72 49 26 30 51 34 55 44 36 50 48 54 32 36 41 55 47 31 25 33 28
[337] 35 45 61 49 56 67 51 62 57 25 26 55 34 46 57 54 25 56 39 36 43 72 39 26
[361] 27 50 60 50 25 62 43 33 60 25 67 26 25 50 51 26 29 51 44 61 54 28 32 45
[385] 50 25 59 52 38 31 29 74 58 59 29 25 26 66 43 53 35 43 45 32 30 34 56 50
[409] 30 48 49 40 39 38 37 61 42 59 28 49 32 47 30 28 47 37 31 38 42 51 31 48
[433] 46 30 25 47 61 68 58 27 28 54 25 43 36 33 53 54 36 64 60 25 48 48 40 51
[457] 26 37 50 57 50 53 29 62 68 28 53 57 43 43 64 37 55 53 50 39 46 28 29 26
[481] 38 34 44 52 57 53 28 49 47 30 50 65 33 66 44 46 49 44 28 40 42 39 33 55
[505] 25 56 39 57 66 58 28 62 31 28 27 58 27 27 46 59 41 65 47 67 35 41 60 29
[529] 36 43 52 39 64 50 29 54 38 55 40 57 26 50 51 33 39 64 42 58 33 49 54 45
[553] 37 63 37 56 56 38 47 29 33 40 36 51 56 43 44 30 51 43 38 43 26 43 53 60
[577] 30 26 59 28 54 39 39 37 26 45 35 33 45 62 32 52 56 50 52 46 44 46 66 69
[601] 46 32 50 54 67 46 31 51 33 34 46 56 58 52 50 30 60 25 47 34 32 47 34 37
[625] 32 46 50 29 26 44 31 62 29 39 27 48 52 47 57 42 55 39 63 56 26 27 40 30
[649] 26 50 68 68 33 55 34 29 42 39 43 44 47 69 41 53 55 49 68 39 37 50 50 54
[673] 47 65 55 36 31 68 53 46 38 51 57 34 42 39 47 31 44 55 29 25 28 27 49 43
[697] 53 33 57 61 55 25 50 33 35 50 54 57 44 51 46 26 63 31 56 70 27 28 46 42
[721] 25 44 58 46 33 45 63 54 32 54 45 57 62 52 53 40 58 60 63 67 50 62 59 29
[745] 48 66 48 61 37 27 69 28 40 54 39 29 39 48 25 50 66 56 52 70 60 45 34 54
[769] 39 52 65 55 52 57 30 60 48 49 41 49 38 67 42 26 50 51 36 32 53 29 39 29
[793] 31 50 48 28 31 45 29 45 51 68 41 51 58 55 56 51 50 34 65 43 45 62 34 40
[817] 56 52 33 29 30 50 56 31 47 45 63 48 35 54 42 34 40 48 48 46 56 26 51 48
[841] 29 50 45 39 35 52 65 26 58 28 44 50 39 38 54 56 32 33 28 28 58 26 49 27
[865] 31 26 46 27 32 58 27 25 46 27 45 48 47 32 53 35 29 43 41 54 56 36 32 62
[889] 28 38 34 64 34 53 68 50 44 35 34 37 32 39 33 30 33 26 31 40 53 31 25 52
[913] 48 43 43 27 27 39 65 40 47 62 45 28 70 25 41 45 25 54 50 27 26 54 57 61
[937] 28 41 25 25 61 70 74 37 28 41 31 47 45 58 34 30 40 37 32 55 34 37 43 39
[961] 53 34 55 66 51 46 26 49 51 62 54 49 32 47 47 30 25 61 48 30 51 59 29 46
[985] 54 42 34 28 35 30 63 52 26 28 46 53 27 54 47 36 37 41 48 56 33 31 28 26
[1009] 31 27 28 38 46 32 50 31 51 46 48 41 35 30 50 53 45 28 53 42 41 57 31 25
[1033] 37 32 54 26 51 42 47 55 26 35 62 49 26 49 41 39 54 42 50 25 40 48 29 65
[1057] 44 48 69 29 28 50 43 45 56 40 50 58 42 72 51 45 41 55 33 38 30 25 62 53
[1081] 27 35 27 59 31 26 35 61 72 50 49 67 50 52 45 58 50 57 42 40 45 25 53 51
[1105] 60 34 26 51 55 32 34 59 26 29 34 48 26 37 40 35 50 37 48 40 65 33 38 43
[1129] 27 48 37 41 26 53 36 47 30 28 34 35 25 30 34 50 32 53 56 29 60 61 25 30
[1153] 50 53 33 61 47 25 36 55 28 36 32 30 26 28 31 62 42 38 64 66 46 49 35 31
[1177] 43 38 72 41 28 47 26 34 31 43 27 36 49 69 31 55 28 62 52 30 52 42 48 39
[1201] 26 60 72 41 49 34 54 45 55 42 53 41 50 61 48 66 42 27 44 48 39 57 49 55
[1225] 33 66 56 38 53 46 46 70 41 56 35 28 66 61 48 30 35 55 30 40 36 32 69 28
[1249] 31 52 40 59 32 35 33 32 51 46 42 46 34 38 57 59 25 43 47 64 53 62 36 60
[1273] 58 49 43 48 45 35 42 42 65 60 48 63 27 45 39 35 58 33 46 25 55 58 36 25
[1297] 48 29 41 45 48 31 59 34 52 49 48 62 36 57 27 42 45 40 51 51 48 30 46 46
[1321] 47 56 25 62 45 26 41 34 46 38 45 55 25 28 30 26 32 49 39 35 46 28 49 40
[1345] 32 49 53 48 34 33 53 27 32 43 55 32 74 50 32 61 66 40 46 47 61 28 41 36
[1369] 47 48 46 25 44 34 30 28 43 25 46 55 46 31 46 54 37 39 58 29 44 33 38 47
[1393] 68 27 56 25 29 25 38 26 58 56 55 31 37 36 44 55 51 30 48 65 42 43 56 74
[1417] 39 47 42 30 27 28 43 43 65 26 31 27 40 34 46 49 42 32 31 26 28 44 41 33
[1441] 43 67 26 34 43 40 29 39 28 51 50 44 44 26 52 57 44 39 41 37 35 56 40 34
[1465] 62 52 54 49 56 68 41 30 46 51 25 35 43 62 48 53 49 32 54 39 41 48 74 47
[1489] 32 37 47 25 69 42 50 32 28 42 45 28 32 42 31 39 36 29 25 36 51 29 42 30
[1513] 28 36 34 43 40 37 34 38 55 45 41 29 64 53 55 33 40 50 30 36 43 26 45 35
[1537] 29 44 25 33 48 57 53 43 45 39 45 32 32 25 37 50 34 31 37 52 48 25 57 27
[1561] 40 50 51 61 67 74 69 27 53 34 63 42 53 28 38 59 28 27 33 54 54 26 32 56
[1585] 33 43 50 29 32 35 65 49 36 40 42 31 34 35 32 36 39 53 27 25 41 72 40 57
[1609] 45 60 36 47 33 39 39 31 50 55 34 69 25 64 42 47 45 47 51 68 29
$race
[1] 1 0 1 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[38] 0 0 0 0 0 0 0 0 0 0 0 1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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
[1518] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 1 0 0 0 0 0 1 0 0 1 0 0 0
[1555] 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 0 0 1
[1592] 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[1629] 0
$smokeyrs
[1] 29 24 26 53 19 21 39 9 37 25 24 20 19 38 41 24 44 30 27 31 7 18 11 8
[25] 39 10 23 38 16 21 35 35 16 21 40 14 15 22 10 10 44 21 43 26 22 29 12 28
[49] 9 32 23 12 14 28 26 20 39 24 34 34 26 12 38 7 14 13 21 26 15 24 36 2
[73] 23 21 36 55 12 20 47 8 16 17 18 24 49 16 30 39 40 17 17 20 9 16 26 40
[97] 34 14 33 32 34 36 27 17 14 14 25 22 36 31 30 6 40 16 8 25 52 45 42 16
[121] 33 22 7 49 3 28 23 18 20 31 38 10 42 14 23 15 42 13 16 25 26 33 12 14
[145] 17 22 14 17 32 39 18 16 43 37 36 37 57 35 51 9 38 31 10 47 16 14 43 17
[169] 31 32 22 21 38 19 36 8 46 36 10 35 45 6 19 37 21 6 53 23 23 34 51 14
[193] 41 25 11 42 31 11 12 29 39 15 26 38 14 33 32 49 16 37 33 40 3 34 27 7
[217] 27 35 20 17 15 14 40 54 28 22 43 39 26 5 24 27 46 32 9 24 19 10 39 24
[241] 26 18 42 20 21 17 26 18 18 33 26 42 14 30 35 42 38 50 18 22 25 12 40 32
[265] 26 20 21 8 10 35 40 26 28 28 45 41 15 37 50 58 30 14 24 30 18 38 24 40
[289] 39 18 43 33 17 56 41 27 32 47 10 38 49 13 23 17 28 29 43 38 30 13 26 33
[313] 3 23 14 41 30 10 12 21 17 39 23 20 34 27 39 13 11 19 40 30 17 6 15 6
[337] 7 20 46 33 37 36 30 32 15 4 5 37 18 11 42 34 4 38 21 21 25 52 21 11
[361] 11 31 35 20 16 41 25 17 44 7 25 8 7 25 32 10 5 23 24 32 37 13 18 27
[385] 34 8 47 35 24 13 11 56 40 41 15 7 8 40 23 21 15 30 13 16 12 10 31 36
[409] 12 32 31 15 25 21 3 22 24 55 13 32 14 29 12 9 29 16 12 24 27 33 14 32
[433] 34 14 12 26 40 48 44 11 14 40 16 34 26 14 15 34 20 42 42 4 29 29 24 31
[457] 8 16 35 49 29 38 7 48 50 10 41 39 26 40 28 23 35 37 36 19 28 12 9 8
[481] 21 14 7 35 27 43 16 33 31 12 35 15 19 57 24 21 33 21 11 17 25 22 15 30
[505] 7 41 19 36 40 30 10 27 16 13 13 16 11 7 30 25 21 56 29 53 23 1 46 11
[529] 15 22 37 25 35 26 13 36 20 40 17 37 9 38 16 13 24 34 36 46 11 35 48 26
[553] 19 45 22 46 22 22 28 15 16 21 18 29 42 28 27 4 44 26 14 13 8 15 28 35
[577] 12 8 40 12 32 18 21 21 7 30 17 13 40 46 2 1 36 26 34 28 26 33 48 13
[601] 26 14 32 34 55 28 13 43 19 17 30 37 44 22 32 5 42 5 26 17 16 30 20 19
[625] 16 32 33 13 13 27 17 52 8 26 8 32 31 30 35 26 39 22 44 33 11 9 24 12
[649] 9 29 50 48 13 35 18 9 24 5 29 27 29 54 27 44 34 33 50 21 18 35 38 32
[673] 30 49 35 22 3 38 34 21 20 35 40 13 15 24 28 15 25 37 13 9 7 10 8 28
[697] 35 14 27 31 37 8 35 15 19 30 37 39 9 25 26 10 33 13 35 53 7 10 32 22
[721] 4 24 41 30 14 26 10 34 16 46 30 41 22 27 36 20 36 40 30 55 33 47 39 12
[745] 29 52 28 45 19 7 64 4 20 38 24 14 19 28 8 31 44 30 36 15 40 22 16 30
[769] 24 34 33 41 38 33 12 44 18 32 23 30 22 46 23 8 29 29 21 16 39 13 21 12
[793] 15 33 10 13 13 25 14 30 28 40 26 38 28 39 38 36 12 18 49 2 27 37 15 32
[817] 37 31 15 10 9 32 40 14 27 28 39 32 17 29 22 17 25 32 30 19 36 3 30 27
[841] 9 32 26 19 17 7 51 7 42 10 22 31 17 20 40 32 14 18 12 14 28 8 27 15
[865] 10 9 32 5 17 35 6 5 21 10 26 28 21 14 33 16 10 27 22 40 39 23 16 51
[889] 10 18 22 48 16 33 54 32 26 19 18 22 13 24 13 13 11 9 6 21 23 14 8 32
[913] 32 23 23 11 10 21 47 24 29 42 25 3 40 7 23 15 14 37 4 9 13 32 38 41
[937] 11 23 8 7 44 56 36 17 10 29 13 27 29 40 4 18 17 16 13 33 10 20 28 22
[961] 37 24 35 31 24 27 9 27 31 49 35 20 14 31 28 10 7 41 26 9 30 37 11 25
[985] 38 24 16 10 17 17 43 40 12 8 30 33 9 39 31 21 20 23 33 35 8 15 8 4
[1009] 15 9 10 16 27 12 28 14 34 33 30 25 20 15 28 35 27 10 40 20 25 29 13 9
[1033] 19 17 44 9 31 19 29 12 9 15 46 31 15 30 6 18 36 6 30 7 25 31 17 48
[1057] 22 28 39 12 12 35 25 17 36 25 40 41 31 52 39 27 15 38 19 12 10 10 55 29
[1081] 10 18 9 46 15 20 20 21 58 35 33 38 29 37 15 30 31 39 21 26 29 10 32 31
[1105] 48 16 11 31 35 14 19 45 8 11 13 30 7 16 20 18 37 24 13 19 50 14 22 28
[1129] 12 10 14 16 9 32 30 29 17 6 16 16 5 14 14 32 16 32 40 9 40 44 6 7
[1153] 41 34 13 55 27 1 20 43 15 15 15 5 10 11 13 23 27 21 43 48 27 26 15 17
[1177] 29 21 27 27 12 25 4 16 13 25 7 16 31 24 12 36 9 47 37 16 34 21 23 26
[1201] 13 30 60 29 29 13 37 20 37 17 28 23 32 42 32 48 25 9 30 31 22 45 22 38
[1225] 15 48 38 19 32 31 28 58 24 38 18 10 51 45 26 11 18 38 12 20 11 17 42 7
[1249] 13 35 10 41 15 15 18 14 30 21 26 24 12 20 39 45 6 25 30 42 41 45 11 30
[1273] 40 29 24 31 27 22 21 21 48 40 29 43 12 30 23 17 33 18 25 7 25 35 19 8
[1297] 36 10 21 26 31 22 41 16 30 29 14 48 1 43 6 30 15 23 28 32 26 15 37 23
[1321] 26 38 5 50 30 5 21 26 29 23 29 43 10 17 11 11 12 37 18 17 21 12 30 25
[1345] 18 32 39 33 2 15 32 9 10 19 22 13 44 32 9 45 45 29 26 31 16 13 27 19
[1369] 31 27 29 9 35 20 16 12 18 8 32 35 26 15 32 33 20 23 29 11 26 15 20 24
[1393] 40 11 11 8 15 11 21 10 38 44 37 8 19 26 24 41 33 13 30 41 21 28 35 49
[1417] 21 29 22 14 8 8 23 26 47 10 15 9 24 12 28 31 29 16 14 8 4 28 21 20
[1441] 22 45 12 19 27 35 11 21 15 35 33 28 26 7 32 43 28 24 23 20 15 44 30 19
[1465] 41 36 32 17 35 54 23 12 30 35 7 14 22 44 29 13 29 15 38 23 21 26 54 29
[1489] 18 21 31 5 34 28 32 13 13 27 29 9 4 23 15 22 14 13 13 16 35 15 25 14
[1513] 11 14 4 18 21 12 21 22 36 24 20 12 40 27 43 15 22 34 11 19 24 11 35 17
[1537] 14 24 12 16 30 36 30 27 27 23 30 16 11 4 14 31 19 15 17 38 31 10 39 7
[1561] 25 35 16 49 51 53 52 6 37 21 44 25 37 12 19 38 18 10 16 36 36 12 12 38
[1585] 17 27 27 13 19 13 40 32 19 23 22 13 19 15 12 1 21 33 5 13 13 7 24 37
[1609] 33 50 19 28 17 26 25 19 32 39 16 34 13 30 29 31 29 31 30 46 14
Chapter 6 might have some hints on a quick way to do so.
None of these variables has any missing data, but if you don’t trust me, check for yourself!
First, though, take a minute to look through the list object and make note of a few things. All we’ve done is taken each variable from the nh
dataframe and stored it as a separate vector in the list called nhl
.
No observations have been reordered, so if we took the 7th value from each vector stored in nhl
, we should be able to reproduce the 7th row of nh
.
unlist(lapply(nhl, function(x) x[7]))
list7 <- unlist(nh[7, ])
df7 <-
cbind(list7, df7)
list7 df7
seqn 262 262
death 0 0
sex 1 1
age 56 56
race 0 0
smokeyrs 39 39
smokeintensity 20 20
marital 2 2
So far, so good.
Before we start writing up our code to generate some descriptive statistics for these variables, we should list the tasks we’ll need to execute in order to do so.
You may have noticed in the header of nh
that all of the variables are labeled as dbl
, meaning they are numeric. However, we know that some of them should (or could) be treated as categorical.
Just to be safe, we can check to make sure the variables were saved as numeric in the list as well.
cbind(unlist(lapply(nhl, class)))
[,1]
seqn "numeric"
death "numeric"
sex "numeric"
age "numeric"
race "numeric"
smokeyrs "numeric"
smokeintensity "numeric"
marital "numeric"
With each variable class established, we might decide we need to complete the following list of tasks:
Inspect each variable and calculate the appropriate descriptive statistics. For the current example, we’ll just say we want a frequency table for categorical variables and a five-number summary for continuous variables.
Label each variable according to how we treated it. As an arbitrary rule, we’ll consider variables with fewer than 6 unique values to be categorical and all others to be continuous.
Store the results.
Considering this problem will allow us to introduce a couple of looping approaches we might consider. The first is a self-written loop similar to the one we already wrote. The next couple of approaches will used vectorized loops, either with functions provided as part of the standard R installation or with some newer functions provided via the purrr
package.
To begin, we’re going to create a new list nhl_sum
in which each of its elements is itself a list storing the original variable vector, the variable’s class type, and the summary statistics we calculate.
list()
nhl_sum <-
for (i in 1:length(nhl)) {
# reinitialize an empty temporary list at the beginning of each iteration
list()
templist <-
# test the number of unique values in the ith element of nhl
# if there are 6 or fewer unique values, treat as categorical
# otherwise, treat as continuous
if (length(unique(nhl[[i]])) <= 6) {
"type"]] <- "categorical"
templist[["stats"]] <- table(nhl[[i]])
templist[[
else {
}
"type"]] <- "continuous"
templist[["stats"]] <- summary(nhl[[i]])
templist[[
}
# save the list we created into the ith element of our final holding object
templist
nhl_sum[[i]] <-
# name the list element
names(nhl_sum)[i] <- names(nhl)[i]
}
print(nhl_sum)
$seqn
$seqn$type
[1] "continuous"
$seqn$stats
Min. 1st Qu. Median Mean 3rd Qu. Max.
233 10607 20333 16552 22719 25061
$death
$death$type
[1] "categorical"
$death$stats
0 1
1311 318
$sex
$sex$type
[1] "categorical"
$sex$stats
0 1
799 830
$age
$age$type
[1] "continuous"
$age$stats
Min. 1st Qu. Median Mean 3rd Qu. Max.
25.00 33.00 44.00 43.92 53.00 74.00
$race
$race$type
[1] "categorical"
$race$stats
0 1
1414 215
$smokeyrs
$smokeyrs$type
[1] "continuous"
$smokeyrs$stats
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.00 15.00 24.00 24.87 33.00 64.00
$smokeintensity
$smokeintensity$type
[1] "continuous"
$smokeintensity$stats
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.00 10.00 20.00 20.55 30.00 80.00
$marital
$marital$type
[1] "categorical"
$marital$stats
2 3 4 5 6 8
1279 97 96 99 57 1
Note that we included seqn
, which is the recipient ID. We could have written the loop to omit that variable, or we could have dropped its summary from nhl_sum
.
If we just want to peak at the smokeintensity
statistics, for instance, we can access them directly from nhl_sum
:
$smokeintensity nhl_sum
$type
[1] "continuous"
$stats
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.00 10.00 20.00 20.55 30.00 80.00
We’re not going to focus properly on vectorization, except to review an implementation or two from what’s called the apply
family of functions in R. In general, vectorized loops can operate quite a bit faster in certain circumstances, but they also provide a different approach to iteration.
To go beyond the looping examples covered here, DataCamp has a detailed tutorial on writing loops and controlling their flow and output.
For instance, let’s achieve something similar to what we did in the prior section with a standard for
loop. With the for
loop, we had to tell R explicitly how many loops to do, and we had to tell it where to store our results in nhl_sum
each time.
We might see some advantages to this approach: for one, we can read the code line by line and see the entire sequence of events. We also forced ourselves to be exact about what R was to do with each piece of information, which is good practice.
But R gives us a variety of flexible tools to work with, and the apply
functions can provide some more elegant, less verbose,1 “Verbose” essentially means we have to type a lot. You may also see functions use a setting called “verbose” to control how much output a function gives you, so the word is used as a bit of jargon in programming. and easier-to-debug solutions.
Anyway, since this introduction to vectorized loops is getting a little verbose, let’s skip to the good stuff: we can use the lapply
function to get a similar result as we did with a for
loop.
lapply(nhl, function(x) {
nhl_sum2 <-
if (length(x) <= 6) {
list(type = "categorical",
stats = table(x))
else {
} list(type = "continuous",
stats = summary(x))
}
})
print(nhl_sum2)
$seqn
$seqn$type
[1] "continuous"
$seqn$stats
Min. 1st Qu. Median Mean 3rd Qu. Max.
233 10607 20333 16552 22719 25061
$death
$death$type
[1] "continuous"
$death$stats
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.0000 0.0000 0.0000 0.1952 0.0000 1.0000
$sex
$sex$type
[1] "continuous"
$sex$stats
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.0000 0.0000 1.0000 0.5095 1.0000 1.0000
$age
$age$type
[1] "continuous"
$age$stats
Min. 1st Qu. Median Mean 3rd Qu. Max.
25.00 33.00 44.00 43.92 53.00 74.00
$race
$race$type
[1] "continuous"
$race$stats
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.000 0.000 0.000 0.132 0.000 1.000
$smokeyrs
$smokeyrs$type
[1] "continuous"
$smokeyrs$stats
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.00 15.00 24.00 24.87 33.00 64.00
$smokeintensity
$smokeintensity$type
[1] "continuous"
$smokeintensity$stats
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.00 10.00 20.00 20.55 30.00 80.00
$marital
$marital$type
[1] "continuous"
$marital$stats
Min. 1st Qu. Median Mean 3rd Qu. Max.
2.000 2.000 2.000 2.503 2.000 8.000
Remember, what we wrote above is also a loop, but we can translate roughly into words what we told R to do:
nhl
, and apply a function to it.type
(set to “categorical”) and a frequency table stored in stats
.type
(set to “continuous”) and the results of summary()
stored in stats
.The “l” in lapply
refers to “list”, meaning the function will return to us a list. In each drawer will be stored the results of each iteration of the function—in other words, one of the lists we told R to make in the if-else statements.
We may look at the code above and concede it is more succinct than the standard for
loop, but maybe it’s a tad ugly because we specified an anonymous function2 Jargon alert: An anonymous function simply means that the function is not named or stored in our environment. within the lapply
function itself.
We could have done the following for the sake of readability:
function(x) {
autosummary <-
if (length(x) <= 6) {
list(type = "categorical",
stats = table(x))
else {
} list(type = "continuous",
stats = summary(x))
}
}
lapply(nhl, autosummary)
nhl_sum3 <-print(nhl_sum3)
$seqn
$seqn$type
[1] "continuous"
$seqn$stats
Min. 1st Qu. Median Mean 3rd Qu. Max.
233 10607 20333 16552 22719 25061
$death
$death$type
[1] "continuous"
$death$stats
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.0000 0.0000 0.0000 0.1952 0.0000 1.0000
$sex
$sex$type
[1] "continuous"
$sex$stats
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.0000 0.0000 1.0000 0.5095 1.0000 1.0000
$age
$age$type
[1] "continuous"
$age$stats
Min. 1st Qu. Median Mean 3rd Qu. Max.
25.00 33.00 44.00 43.92 53.00 74.00
$race
$race$type
[1] "continuous"
$race$stats
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.000 0.000 0.000 0.132 0.000 1.000
$smokeyrs
$smokeyrs$type
[1] "continuous"
$smokeyrs$stats
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.00 15.00 24.00 24.87 33.00 64.00
$smokeintensity
$smokeintensity$type
[1] "continuous"
$smokeintensity$stats
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.00 10.00 20.00 20.55 30.00 80.00
$marital
$marital$type
[1] "continuous"
$marital$stats
Min. 1st Qu. Median Mean 3rd Qu. Max.
2.000 2.000 2.000 2.503 2.000 8.000
Other functions in the apply
family include apply
itself, sapply
, mapply
, and tapply
. Each provides variations on a theme, handling different sorts of inputs and returning different object types.
A full review of the apply
family is beyond the scope of this introduction, and to be honest, while your humble author uses several of these functions all the time, he will never really remember how to use the base apply
function properly.
In fact, you’ve already seen some of these functions used in prior code examples that were focused on other aspects of R. Scroll through prior chapters and sections to see if you can spot them and figure out what they’re doing.
Nesting in this context refers to placing loops within loops.3 Kind of like dreams within dreams in the movie Inception, except nested loops make sense!
Loop logic takes a while to get used to, and you might find you don’t need them all at often. Nonetheless, they’re something we should make an attempt to familiarize outselves with.
Perhaps the following code will say in generated output what is difficult to say in words:
LETTERS[1:4]
alpha <- 1:10
nums <-
for (i in 1:length(alpha)) {
cat("<<", alpha[i], ">>\n")
for (j in 1:length(nums)) {
cat(paste0(alpha[i], nums[j]), " ")
}
cat("\n\n")
}
<< A >>
A1 A2 A3 A4 A5 A6 A7 A8 A9 A10
<< B >>
B1 B2 B3 B4 B5 B6 B7 B8 B9 B10
<< C >>
C1 C2 C3 C4 C5 C6 C7 C8 C9 C10
<< D >>
D1 D2 D3 D4 D5 D6 D7 D8 D9 D10
In words, we loop through two objects, alpha
and nums
, tracked by i
and j
, respectively.
We get to the first element of alpha
, output it to the console using cat()
, and then, we loop through nums
, paste each object to the current alpha
element, and output that string to the console. Once we’ve finished doing this for all the elements of num
, we move to the next iteration and continue until we’ve exhausted the whole procedure for all elements in alpha
.