**Copy and paste the following code to your R Studio platform or R version:**

# Functions in R – a sequence of statements grouped together for a purpose

# We do not need to re-write the code, just to call the function each time

# we need. So, we save time and code lines by using functions the right way

vec1 <- c(1:10)

print(vec1) # an example of in-built function

head(vec1, 5) # another example

# In constrast, we have the user-defined functions

print.func <- function() {

print(“My first function”)

}

print.func()

add.function <- function(a, b) {

a + b

}

add.function(3, 5)

add.function(10, 20)

add.function(30, -30)

operations.function <- function(x, a, b) {

switch(x, a + b, a – b, a * b, a / b)

}

# the x parameter of the function determines which operation is going

# to be operated – for instance, 1 for addition, 3 for multiplication

operations.function(1, 2, 3)

operations.function(4, 9, 3)

# default function arguments

operations.function <- function(x, a, b = 10) {

switch(x, a + b, a – b, a * b, a / b)

}

operations.function(3, 3)

# Alternative loops in R

x <- rnorm(100000, mean = 0, sd = 1)

head(x)

length(x)

y <- rnorm(100000, mean = 10, sd = 2)

head(y)

length(y)

combined.df <- data.frame(x,y)

head(combined.df)

str(combined.df)

mean(combined.df$x)

mean(combined.df$y)

combined.copy <- combined.df

# let us log the time of the process to understand how long it takes

start <- proc.time()

for(i in 1:100000) {

for(j in 1:2) {

combined.copy[i,j] <- combined.copy[i,j] + 1

}

}

end <- proc.time() – start

print(end)

# It took almost 2 minutes in my machine

# Let’s check the outcome

head(combined.copy)

head(combined.df)

# If we exploit the vectorized nature of R, the same process takes

# merely a second – the difference is indeed stunning

combined.copy.R <- combined.df + 1

head(combined.copy.R)

head(combined.df)

# ifelse construct

new.df <- combined.df

new.df$flag <- ifelse(combined.df$x < 1, “0”, “1”)

# again, this vectorized operation was really fast

head(new.df)

# The apply function family in R

# Write complex code in a simple statement

help(“apply”)

x <- rnorm(100000, mean = 0, sd = 1)

head(x)

length(x)

y <- rnorm(100000, mean = 10, sd = 2)

head(y)

length(y)

combined.df <- data.frame(x,y)

head(combined.df)

str(combined.df)

mean(combined.df$x)

mean(combined.df$y)

combined.copy <- combined.df

mean <- apply(combined.copy, 2, mean)

# 1 for rows and 2 for columns

print(mean)

print(mean(combined.copy$x))

z <- c(2,3)

output.apply <- t(apply(combined.copy, 1, function(combined.copy, z){combined.copy + z}, z))

# the reason for transposing the data frame is because the output if we not transpose it

# is not what we want to achieve, since we want to add the number 2 to the x column and

# the number 3 to the y column and store it as a data frame with 2 columns, meaning that we

# keep the same number of columns and this is the case only when we use the t function

head(output.apply)

head(combined.df)