R Intermediate

🚀 Welcome to R Intermediate! Now that you’ve mastered the basics, it’s time to take your R skills to the next level. In this section, you’ll dive into Indexing, Conditional Statements, and Loops—core tools for writing efficient and adaptable code. These concepts are essential for data manipulation, automation, and building reproducible analysis workflows. Let’s build on your foundation and start coding with more precision and control!

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