In this section, we will cover the fundamental operations and functions in R. Understanding these basics is crucial for performing more complex tasks later on. We will explore arithmetic operations, logical operations, and how to use built-in functions in R.

  1. Arithmetic Operations

R supports standard arithmetic operations, which are similar to those in other programming languages. Here are the basic arithmetic operators:

Operator Description Example Result
+ Addition 5 + 3 8
- Subtraction 5 - 3 2
* Multiplication 5 * 3 15
/ Division 5 / 3 1.67
^ Exponentiation 5 ^ 3 125
%% Modulus (remainder) 5 %% 3 2
%/% Integer Division 5 %/% 3 1

Example

# Basic arithmetic operations
a <- 10
b <- 3

sum <- a + b
difference <- a - b
product <- a * b
quotient <- a / b
power <- a ^ b
remainder <- a %% b
int_division <- a %/% b

# Print results
print(paste("Sum:", sum))
print(paste("Difference:", difference))
print(paste("Product:", product))
print(paste("Quotient:", quotient))
print(paste("Power:", power))
print(paste("Remainder:", remainder))
print(paste("Integer Division:", int_division))

  1. Logical Operations

Logical operations are used to compare values and return TRUE or FALSE. Here are the basic logical operators:

Operator Description Example Result
== Equal to 5 == 3 FALSE
!= Not equal to 5 != 3 TRUE
> Greater than 5 > 3 TRUE
< Less than 5 < 3 FALSE
>= Greater than or equal to 5 >= 3 TRUE
<= Less than or equal to 5 <= 3 FALSE
& AND TRUE & FALSE FALSE
` ` OR `TRUE
! NOT !TRUE FALSE

Example

# Logical operations
x <- 5
y <- 3

equal <- x == y
not_equal <- x != y
greater_than <- x > y
less_than <- x < y
greater_equal <- x >= y
less_equal <- x <= y
and_operation <- (x > 2) & (y < 5)
or_operation <- (x > 2) | (y > 5)
not_operation <- !(x == y)

# Print results
print(paste("Equal:", equal))
print(paste("Not Equal:", not_equal))
print(paste("Greater Than:", greater_than))
print(paste("Less Than:", less_than))
print(paste("Greater or Equal:", greater_equal))
print(paste("Less or Equal:", less_equal))
print(paste("AND Operation:", and_operation))
print(paste("OR Operation:", or_operation))
print(paste("NOT Operation:", not_operation))

  1. Built-in Functions

R has a rich set of built-in functions that make it easy to perform various tasks. Here are some commonly used functions:

Function Description Example Result
sum() Sum of elements sum(1, 2, 3) 6
mean() Mean of elements mean(c(1, 2, 3)) 2
max() Maximum value max(c(1, 2, 3)) 3
min() Minimum value min(c(1, 2, 3)) 1
sqrt() Square root sqrt(16) 4
abs() Absolute value abs(-5) 5

Example

# Using built-in functions
numbers <- c(1, 2, 3, 4, 5)

total <- sum(numbers)
average <- mean(numbers)
maximum <- max(numbers)
minimum <- min(numbers)
square_root <- sqrt(16)
absolute_value <- abs(-5)

# Print results
print(paste("Sum:", total))
print(paste("Mean:", average))
print(paste("Max:", maximum))
print(paste("Min:", minimum))
print(paste("Square Root of 16:", square_root))
print(paste("Absolute Value of -5:", absolute_value))

  1. Creating and Using Functions

Creating your own functions in R is straightforward. Functions allow you to encapsulate code for reuse and better organization.

Syntax

function_name <- function(arg1, arg2, ...) {
  # Function body
  # Perform operations
  return(result)
}

Example

# Define a function to calculate the area of a rectangle
calculate_area <- function(length, width) {
  area <- length * width
  return(area)
}

# Use the function
length <- 5
width <- 3
area <- calculate_area(length, width)

# Print result
print(paste("Area of the rectangle:", area))

Practical Exercises

Exercise 1

Write a function calculate_circumference that takes the radius of a circle as an argument and returns the circumference. Use the formula circumference = 2 * pi * radius.

Solution

calculate_circumference <- function(radius) {
  circumference <- 2 * pi * radius
  return(circumference)
}

# Test the function
radius <- 4
circumference <- calculate_circumference(radius)
print(paste("Circumference of the circle:", circumference))

Exercise 2

Write a function is_even that takes an integer as an argument and returns TRUE if the number is even and FALSE if it is odd.

Solution

is_even <- function(number) {
  return(number %% 2 == 0)
}

# Test the function
number <- 7
result <- is_even(number)
print(paste("Is the number even?", result))

Exercise 3

Create a function calculate_bmi that takes weight (in kg) and height (in meters) as arguments and returns the Body Mass Index (BMI). Use the formula BMI = weight / (height^2).

Solution

calculate_bmi <- function(weight, height) {
  bmi <- weight / (height^2)
  return(bmi)
}

# Test the function
weight <- 70
height <- 1.75
bmi <- calculate_bmi(weight, height)
print(paste("BMI:", bmi))

Conclusion

In this section, we covered the basic arithmetic and logical operations in R, explored some of the built-in functions, and learned how to create and use custom functions. These foundational skills are essential for performing more complex tasks in R. In the next section, we will delve into data types and structures, which are crucial for effective data manipulation and analysis.

© Copyright 2024. All rights reserved