For, while loop
- for each tree (1 to 5), print the ages.
- for each tree, print the age, in which they were small.
- load esoph dataset.
- Using for loop(s), summarize ncontrols by agegp and alcgp, skipping the category alcgp==“120+” & agegp==“25-34”.
- Using a for loop simulate the flip a coin twenty times, keeping track of the individual outcomes (1 = heads, 0 = tails) in a vector that you preallocte.
- Use a while loop to investigate the number of terms required before the product 1234… reaches above 10 million
- Use a nested for loop (a for loop inside a for loop) that produces the following matrix, preallocate the matrix with NA values.
Orange[Orange$Tree==1,"age"]
## [1] 118 484 664 1004 1231 1372 1582
for (i in 1:5){
cat("Tree: ", i, " Ages: ", paste0(Orange[Orange$Tree==i,"age"], collapse = ", "), "\n")
}
## Tree: 1 Ages: 118, 484, 664, 1004, 1231, 1372, 1582
## Tree: 2 Ages: 118, 484, 664, 1004, 1231, 1372, 1582
## Tree: 3 Ages: 118, 484, 664, 1004, 1231, 1372, 1582
## Tree: 4 Ages: 118, 484, 664, 1004, 1231, 1372, 1582
## Tree: 5 Ages: 118, 484, 664, 1004, 1231, 1372, 1582
for (i in 1:5){
cat("Tree: ", i, " was small at age: ", paste0(Orange[Orange$Tree==i & Orange$cc=="small","age"], collapse = ", "), "\n")
}
## Tree: 1 was small at age: 118, 484
## Tree: 2 was small at age: 118
## Tree: 3 was small at age: 118, 484
## Tree: 4 was small at age: 118, 484
## Tree: 5 was small at age: 118, 484
# 0 1 2 3 4
# 1 0 1 2 3
# 2 1 0 1 2
# 3 2 1 0 1
# 4 3 2 1 0
data("esoph")
for (agegrp in levels(esoph$agegp)){
print(sum(esoph$ncontrols[esoph$agegp==agegrp]))
}
## [1] 115
## [1] 190
## [1] 167
## [1] 166
## [1] 106
## [1] 31
for (agegrp in levels(esoph$agegp)){
for (alcgrp in levels(esoph$alcgp)){
#print(agegrp)
#print(alcgrp)
cat("Age group: ", agegrp, ", alcohol consumption group: ", alcgrp, " number of controls: ", sum(esoph$ncontrols[esoph$alcgp==alcgrp & esoph$agegp==agegrp]), "\n")
}
}
## Age group: 25-34 , alcohol consumption group: 0-39g/day number of controls: 61
## Age group: 25-34 , alcohol consumption group: 40-79 number of controls: 45
## Age group: 25-34 , alcohol consumption group: 80-119 number of controls: 5
## Age group: 25-34 , alcohol consumption group: 120+ number of controls: 4
## Age group: 35-44 , alcohol consumption group: 0-39g/day number of controls: 88
## Age group: 35-44 , alcohol consumption group: 40-79 number of controls: 76
## Age group: 35-44 , alcohol consumption group: 80-119 number of controls: 20
## Age group: 35-44 , alcohol consumption group: 120+ number of controls: 6
## Age group: 45-54 , alcohol consumption group: 0-39g/day number of controls: 77
## Age group: 45-54 , alcohol consumption group: 40-79 number of controls: 61
## Age group: 45-54 , alcohol consumption group: 80-119 number of controls: 27
## Age group: 45-54 , alcohol consumption group: 120+ number of controls: 2
## Age group: 55-64 , alcohol consumption group: 0-39g/day number of controls: 77
## Age group: 55-64 , alcohol consumption group: 40-79 number of controls: 62
## Age group: 55-64 , alcohol consumption group: 80-119 number of controls: 19
## Age group: 55-64 , alcohol consumption group: 120+ number of controls: 8
## Age group: 65-74 , alcohol consumption group: 0-39g/day number of controls: 60
## Age group: 65-74 , alcohol consumption group: 40-79 number of controls: 28
## Age group: 65-74 , alcohol consumption group: 80-119 number of controls: 16
## Age group: 65-74 , alcohol consumption group: 120+ number of controls: 2
## Age group: 75+ , alcohol consumption group: 0-39g/day number of controls: 23
## Age group: 75+ , alcohol consumption group: 40-79 number of controls: 8
## Age group: 75+ , alcohol consumption group: 80-119 number of controls: 0
## Age group: 75+ , alcohol consumption group: 120+ number of controls: 0
for (agegrp in levels(esoph$agegp)){
for (alcgrp in levels(esoph$alcgp)){
if (agegrp=="25-34" & alcgrp=="120+"){
#next
} else {
cat("Age group: ", agegrp, ", alcohol consumption group: ", alcgrp, " number of controls: ", sum(esoph$ncontrols[esoph$alcgp==alcgrp & esoph$agegp==agegrp]), "\n")}
}
}
## Age group: 25-34 , alcohol consumption group: 0-39g/day number of controls: 61
## Age group: 25-34 , alcohol consumption group: 40-79 number of controls: 45
## Age group: 25-34 , alcohol consumption group: 80-119 number of controls: 5
## Age group: 35-44 , alcohol consumption group: 0-39g/day number of controls: 88
## Age group: 35-44 , alcohol consumption group: 40-79 number of controls: 76
## Age group: 35-44 , alcohol consumption group: 80-119 number of controls: 20
## Age group: 35-44 , alcohol consumption group: 120+ number of controls: 6
## Age group: 45-54 , alcohol consumption group: 0-39g/day number of controls: 77
## Age group: 45-54 , alcohol consumption group: 40-79 number of controls: 61
## Age group: 45-54 , alcohol consumption group: 80-119 number of controls: 27
## Age group: 45-54 , alcohol consumption group: 120+ number of controls: 2
## Age group: 55-64 , alcohol consumption group: 0-39g/day number of controls: 77
## Age group: 55-64 , alcohol consumption group: 40-79 number of controls: 62
## Age group: 55-64 , alcohol consumption group: 80-119 number of controls: 19
## Age group: 55-64 , alcohol consumption group: 120+ number of controls: 8
## Age group: 65-74 , alcohol consumption group: 0-39g/day number of controls: 60
## Age group: 65-74 , alcohol consumption group: 40-79 number of controls: 28
## Age group: 65-74 , alcohol consumption group: 80-119 number of controls: 16
## Age group: 65-74 , alcohol consumption group: 120+ number of controls: 2
## Age group: 75+ , alcohol consumption group: 0-39g/day number of controls: 23
## Age group: 75+ , alcohol consumption group: 40-79 number of controls: 8
## Age group: 75+ , alcohol consumption group: 80-119 number of controls: 0
## Age group: 75+ , alcohol consumption group: 120+ number of controls: 0