R Programming Tutorial
Estimated reading: 4 minutes 36 views

🔤 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, and while
  • How to define and reuse functions in your R scripts

📘 Topics Covered

🧠 Topic📖 Description
🔡 R – VariablesDeclaring, assigning, and accessing variables in R.
🧮 R – Data TypesOverview of numeric, character, logical, factor, and complex types.
➕ R – Numbers & MathUsing mathematical operators and functions with numeric data.
🧵 R – StringsWorking with text: concatenation, formatting, and string manipulation.
🔘 R – BooleansUnderstanding TRUE / FALSE values and logical expressions.
⚙️ R – OperatorsUse of arithmetic, logical, relational, and assignment operators.
🔀 R – If…Else / Decision MakingWriting conditional code using if, else, else if, and switch.
🔁 R – Loops (For, While)Iterating over sequences using for, while, and repeat loops.
🧩 R – FunctionsCreating 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

R Core Language Concepts

Or Copy Link

CONTENTS
Scroll to Top