What is R? 📊
R is a programming language created specifically for data analysis, statistics and visualization. It is widely used in science, business and machine learning. 📈
R is popular among scientists, analysts and researchers, as it contains many built-in functions for processing big data. 🎯
Why study R? 🔥
Simplicity: convenient and readable syntax.
Data analysis: built-in tools for working with tables and graphs.
Machine learning: support for libraries for neural networks and predictive analytics.
The wider community: many ready-made packages and documentation.
How to install R? 💻
To work with R you will need:
✅ R interpreter. You can download it from official website.
✅ Code editor. Recommended RStudio - convenient environment for working with R.
How to check if R is installed? 🛠
Open the terminal and enter:
R --versionIf R is installed, its version will appear.
R Syntax Basics 📚
1. Variables 📦
In R, variables are created simply:
Example of declaring variables:
# Creating variables
name <- "Alice" # Line
age <- 25 # Number
pi_value <- 3.14 # Floating point number
# Variable output
print(name)
print(age)
print(pi_value)2. Data output 📤
To display information, use print().
Example:
print("Hello, world!")3. Conditional operators (if...else) 🧐
Allow the program to make decisions.
Example:
age <- 20
if (age >= 18) {
print("You're an adult!")
} else {
print("You're still a child.")
}4. Loops (for, while) 🔄
Loops allow you to repeat actions.
Example:
for (i in 1:5) {
print(i)
}5. Functions 📐
Functions help to structure the code.
Example:
square <- function(x) {
return(x * x)
}
print(square(5))Simple projects on R 💡
1. Random number generator 🎲
The program generates a random number from 1 to 100.
# Random number generation
random_number <- sample(1:100, 1)
print(paste("Random number:", random_number))2. Multiplication table 📊
The program displays a multiplication table from 1 to 10.
for (i in 1:10) {
for (j in 1:10) {
cat(i, "x", j, "=", i * j, "\t")
}
cat("\n")
}3. Counting the number of characters in a string 🔢
The program counts the characters in the string.
text <- "Hello, R!"
print(paste("Number of characters:", nchar(text)))4. Checking the number for evenness 🔍
The program determines whether the number is even.
number <- 42
if (number %% 2 == 0) {
print(paste(number, "- even number"))
} else {
print(paste(number, "- odd number"))
}5. Calculating the factorial of a number 🎯
This code calculates the factorial of a given number.
factorial <- function(n) {
if (n == 0) {
return(1)
} else {
return(n * factorial(n - 1))
}
}
print(paste("Factorial 5:", factorial(5)))Conclusion 🎉
R is a powerful programming language that is easy to learn. We have analyzed its basics: variables, input/output, conditions, loops and functions. Now you can write your first programs! 🚀
The more you practice, the better your code becomes. Experiment, try new tasks and master programming! Good luck learning R! 😊
