Basic Operations | Dataplexa

Basic Operations in R

In this lesson, you will learn how to perform basic operations in R. These operations help you work with numbers, variables, vectors, and data frames.

Basic operations form the foundation for calculations, analysis, and data processing in R. Once you understand these, working with real datasets becomes much easier.


Arithmetic Operations

R supports standard arithmetic operations such as addition, subtraction, multiplication, and division.

These operations are commonly used in calculations, statistics, and data analysis.

a <- 20
b <- 5

a + b
a - b
a * b
a / b

Each operation returns a calculated result immediately.


Power and Modulus Operations

R provides special operators for exponent (power) and remainder (modulus) calculations.

These are useful in mathematical formulas and logical conditions.

a ^ 2
a %% b

The power operator raises a value to a given exponent, while modulus returns the remainder.


Operations on Vectors

R performs operations on vectors element by element.

This feature is called vectorized operation and makes R very powerful.

x <- c(10, 20, 30)
y <- c(1, 2, 3)

x + y
x * y

Each element in x is operated on with the corresponding element in y.


Logical Operations

Logical operations are used to compare values.

They return either TRUE or FALSE.

a > b
a == b
a != b

Logical comparisons are widely used in conditions and filtering data.


Logical Operators

Logical operators combine multiple conditions.

They are important when checking multiple rules at once.

a > 10 & b < 10
a > 10 | b > 10
!(a == b)

These operators help build complex logical expressions.


Operations on Data Frames

You can perform operations directly on columns of a data frame.

This allows fast calculations without writing loops.

df <- data.frame(
  marks1 = c(80, 85, 90),
  marks2 = c(70, 75, 80)
)

df$total <- df$marks1 + df$marks2
df

This creates a new column based on calculated values.


Summary Operations

R provides built-in functions to summarize numeric data.

These functions help you quickly understand your dataset.

values <- c(10, 20, 30, 40)

sum(values)
mean(values)
min(values)
max(values)

Summary operations are commonly used in reports and analysis.


Rounding Operations

R allows you to round numbers for better readability.

This is useful when working with averages or decimals.

number <- 12.5678

round(number, 2)
floor(number)
ceiling(number)

Each function handles rounding differently.


Why Basic Operations Matter

Basic operations are used in almost every R program.

They help clean data, calculate metrics, and prepare datasets for analysis.


📝 Practice Exercises


Exercise 1

Create two numeric variables and perform all arithmetic operations.

Exercise 2

Create two vectors and multiply them element-wise.

Exercise 3

Use logical operators to compare two values.

Exercise 4

Create a data frame and calculate a new column using existing columns.


✅ Practice Answers


Answer 1

x <- 15
y <- 3

x + y
x - y
x * y
x / y

Answer 2

a <- c(2, 4, 6)
b <- c(1, 2, 3)

a * b

Answer 3

x > 10 & y < 5

Answer 4

df <- data.frame(a = c(5, 10), b = c(15, 20))
df$sum <- df$a + df$b
df

What’s Next?

Now that you understand basic operations, the next lesson will focus on conditional statements in R.

Conditional logic helps programs make decisions based on data.