R Intermediate
Summary Table
Summary of the R Intermediate section.
Concept | Example | Description |
---|---|---|
One-Based Indexing | vec[1] |
First element is at index 1 |
Negative Indexing | vec[-1] |
Excludes the first element |
Multi-Level Indexing | lst$name[1] , substr(x, 1, 1) |
Access nested or character-level elements |
Slice Range | vec[2:4] |
Inclusive range from index 2 to 4 |
Slice to End | vec[3:length(vec)] |
Elements from index 3 to end |
Full Slice | vec[] |
All elements |
Index Out of Bounds | vec[10] |
Returns NA if index exceeds length |
Mixed Positive/Negative | vec[c(-1, 2)] |
Error if mixing negative and positive indices |
if Statement |
if (x > 0) { ... } |
Runs block if condition is TRUE |
else Statement |
else { ... } |
Runs if if condition is FALSE |
else if Statement |
else if (x == 0) { ... } |
Adds more conditions |
Logical Operators | & , | , ! |
Combine or negate logical conditions |
Nested Conditionals | if (...) { if (...) { ... }} |
Place one if inside another |
for Loop |
for (i in 1:5) { ... } |
Iterate over values in a vector |
Loop over Characters | for (i in seq_len(nchar(str))) |
Index through a string |
Loop over List | for (name in names(list)) { ... } |
Iterate over names and values in a list |
while Loop |
while (x > 0) { ... } |
Continue loop while condition is TRUE |
break |
if (x == target) break |
Exit loop early |
next |
if (x == skip) next |
Skip current iteration |
Infinite Loop | while (TRUE) { ... } |
Runs forever unless manually stopped |