7  Functions and Looping in R

7.1 Functions

Consists inbuilt functions like sum(), length(), sqrt(),mean(), summary(), View()

sum() Function

The sum() function calculates the total sum of all the elements in a numeric vector.

Code
sum function
# Calculate the sum of two variables
x=5
y=3
z=2
sum(x,y,z)
[1] 10
Code
# Calculate the sum of a numeric vector
numbers <- c(1, 2, 3)
sum(numbers)
[1] 6
length() Function

The length() function returns the number of elements in a vector (or other objects).

Code
length function
# Find the length of a vector
numbers <- c(1, 2, 3, 4, 5)
length(numbers)
[1] 5
sqrt() Function

The sqrt() function calculates the square root of each element in a numeric vector.

Code
square root function
# Calculate square root of a variable
x=25
sqrt(x)
[1] 5
Code
# Calculate the square root of each element in a numeric vector
numbers <- c(1, 4, 9, 16, 25)
sqrt(numbers)
[1] 1 2 3 4 5
mean() Function

The mean() function calculates the arithmetic mean (average) of the elements in a numeric vector.

Code
mean function
# Example: Calculate the mean of a numeric vector
numbers <- c(2,4,6)
mean(numbers)
[1] 4
summary() function

The summary() function in R provides a concise statistical summary of objects like vectors, matrices, data frames, and results of model fitting.

Code
summary function
# Create a numeric vector
vec <- c(1, 2, 3, 4, 5, NA, 7, 8, 9, 10)

# Get summary of the vector
summary(vec)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
  1.000   3.000   5.000   5.444   8.000  10.000       1 
data.frame() function

data.frame() function is used to create data frames, which are table-like structures consisting of rows and columns. - Data frames are one of the most important data structures in R, especially for statistical modeling and data analysis.

Code
creating data frame
# Creating a simple data frame
df <- data.frame(
  Name = c("Alice", "Bob", "Charlie"),
  Age = c(25, 30, 35),
  Height = c(5.5, 6.0, 5.8)
)
df
     Name Age Height
1   Alice  25    5.5
2     Bob  30    6.0
3 Charlie  35    5.8
View() function

View() function is used to invoke a spreadsheet-like data viewer on a data frame, matrix, or other objects that can be coerced into a data frame. - This function is particularly useful during interactive sessions to inspect data visually.

Code
View() function
# View function to see the data in a dedicated window
View(df)
     Name Age Height
1   Alice  25    5.5
2     Bob  30    6.0
3 Charlie  35    5.8

7.2 Loops

Use for, while.

for loop

The for loop in R is used to iterate over a sequence (like a vector or a list) and execute a block of code for each element in the sequence.

Code
for loop
# Print numbers from 1 to 5
for(i in 1:5) {
  print(i)
}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
while loop

The while loop executes a block of code as long as the specified condition is TRUE

Code
while loop
# Print numbers from 1 to 5 using while
i <- 1  # Initialize counter
while(i <= 4) {
  print(i)
  i <- i + 1  # Increment counte
}
[1] 1
[1] 2
[1] 3
[1] 4