🔤 R Core Language Concepts – Master Variables, Data Types, Operators & Control Flow
💡 Learn the essential elements of R that power every script—from variables and data types to logic, loops, and functions!
🧲 Introduction – Build a Strong Foundation in R Syntax and Logic
To write effective R code, you must understand the core language building blocks. These include how to declare and use variables, manage different data types, utilize operators, control execution flow with conditional statements and loops, and encapsulate logic using functions.
Whether you’re working on simple data summaries or advanced statistical models, these basics form the backbone of all R programming.
🎯 What You’ll Learn:
- How to use variables and data types in R
- How to apply arithmetic, logical, and relational operators
- How to implement control structures like
if
,else
,for
, andwhile
- How to define and reuse functions in your R scripts
📘 Topics Covered
🧠 Topic | 📖 Description |
---|---|
🔡 R – Variables | Declaring, assigning, and accessing variables in R. |
🧮 R – Data Types | Overview of numeric, character, logical, factor, and complex types. |
➕ R – Numbers & Math | Using mathematical operators and functions with numeric data. |
🧵 R – Strings | Working with text: concatenation, formatting, and string manipulation. |
🔘 R – Booleans | Understanding TRUE / FALSE values and logical expressions. |
⚙️ R – Operators | Use of arithmetic, logical, relational, and assignment operators. |
🔀 R – If…Else / Decision Making | Writing conditional code using if , else , else if , and switch . |
🔁 R – Loops (For, While) | Iterating over sequences using for , while , and repeat loops. |
🧩 R – Functions | Creating and calling built-in and custom functions in R. |
🔡 R – Variables
Variables in R store data that you can reuse throughout your script.
name <- "Alice"
age <- 30
📌 Use <-
or =
for assignment. Variable names should be meaningful and follow naming conventions.
🧮 R – Data Types
R supports the following primary data types:
- Numeric:
x <- 42
- Character:
name <- "John"
- Logical:
flag <- TRUE
- Factor:
gender <- factor(c("Male", "Female"))
- Complex:
z <- 1 + 2i
Use class(x)
or typeof(x)
to check the type of a variable.
➕ R – Numbers & Math
R has rich support for arithmetic:
a <- 10
b <- 3
result <- a + b # Addition
sqrt(a) # Square root
🧮 Use built-in math functions like log()
, exp()
, round()
for numeric operations.
🧵 R – Strings
Work with textual data using functions like paste()
, substr()
, nchar()
.
greeting <- paste("Hello", "World")
Use sprintf()
for formatting and tolower()
/ toupper()
for case conversion.
🔘 R – Booleans
Boolean values (TRUE
, FALSE
) are used in comparisons and control flows.
x <- 5
y <- 10
x < y # Returns TRUE
Combine conditions using &
, |
, and !
.
⚙️ R – Operators
R supports:
- Arithmetic:
+
,-
,*
,/
,^
- Relational:
==
,!=
,<
,>
,<=
,>=
- Logical:
&
,|
,!
- Assignment:
<-
,=
,->
🔎 Example:
a <- 10
b <- 20
(a < b) & (a != 0)
🔀 R – If…Else / Decision Making
Conditional logic allows your program to decide based on values.
x <- 10
if (x > 5) {
print("Greater than 5")
} else {
print("5 or less")
}
Also use else if
and switch()
for complex conditions.
🔁 R – Loops (For, While)
🔄 For Loop:
for (i in 1:5) {
print(i)
}
🔁 While Loop:
x <- 1
while (x <= 5) {
print(x)
x <- x + 1
}
Use repeat
for infinite loops with break conditions.
🧩 R – Functions
Functions help you modularize code.
add <- function(x, y) {
return(x + y)
}
add(5, 3)
💡 Functions can return values, take default arguments, and even be nested.
📌 Summary – Recap & Next Steps
🔍 Key Takeaways:
- Variables and data types are the backbone of R scripting
- Operators drive mathematical, logical, and relational tasks
- Conditional statements and loops control execution flow
- Functions promote reusable and organized code
⚙️ Real-World Relevance:
These foundational elements are essential for data analysis, automation, and statistical modeling in R.
🎓 Next Steps:
Practice each topic using real datasets. Try creating a script that takes user input, processes data, and returns a formatted result.
❓ Frequently Asked Questions (FAQs)
Q1: What’s the difference between <-
and =
in R?
✅ <-
is the traditional assignment operator in R. =
also works but is more common in function arguments.
Q2: Can I store different data types in one variable?
✅ No, R variables store one data type at a time. Use lists or data frames for mixed types.
Q3: What’s the difference between ==
and =
in R?
✅ ==
checks equality, while =
assigns values (like <-
).
Q4: How do I write multi-line comments in R?
✅ Use #
at the beginning of each line. R doesn’t support block comments.
Q5: Can I define a function inside another function in R?
✅ Yes, R supports nested functions and closures.
Share Now :