top of page
podcasts_512dp.png

Subscribe

CONTACT

  • twitter

Your details were sent successfully!




Welcome to a new chapter on control flow in R. In this chapter, we will explore the if-else statement, which is a fundamental component of flow control in R. The if-else statement allows for the execution of code in a non-linear fashion based on a given condition. It enables you to create dynamic programs that can make decisions, repeat actions, and handle different inputs.

Understanding the If Statement

The if statement evaluates a given condition, and if the condition is true, the block of code within the if statement will be executed. If the condition is false, the code will be ignored. The basic syntax of the if statement is as follows:

if (condition) {
  # Code to execute if condition is true
}

Let's look at an example to better understand the if statement. Suppose we want to check if a number is positive. We can write the following code:

number <- 5

if (number > 0) {
  print("Number is positive")
} else {
  print("Number is negative")
}

In this example, the condition is that the number is greater than zero. If the condition is true, the code within the if block will be executed and "Number is positive" will be printed. If the condition is false, the code within the else block will be executed and "Number is negative" will be printed.

Validating Age for Voting

Let's consider another example where we validate the age for voting. In this case, if a person's age is 18 or above, they are eligible to vote. Otherwise, they are not eligible. Here's the code:

age <- 17

if (age >= 18) {
  print("You are eligible to vote")
} else {
  print("You are not eligible to vote")
}

In this example, if the age is 18 or above, "You are eligible to vote" will be printed. Otherwise, "You are not eligible to vote" will be printed.

Checking for Non-Negative Numbers

Now let's demonstrate the use of the if statement to check whether a number is non-negative (positive or zero). Here's the code:

number <- -10

if (number >= 0) {
  print("Number is a non-negative number")
} else {
  print("Number is a negative number")
}

In this example, if the number is greater than or equal to zero, "Number is a non-negative number" will be printed. Otherwise, "Number is a negative number" will be printed.

Validating Password Length

Let's consider a basic example to validate the length of a password. Here's the code:

password <- "password123"

if (nchar(password) >= 8) {
  print("Password length is valid")
} else {
  print("Password length is invalid. It must be at least eight characters long.")
}

In this example, if the length of the password is greater than or equal to eight, "Password length is valid" will be printed. Otherwise, "Password length is invalid. It must be at least eight characters long." will be printed.

Changing Variable to Factor Data Type

In this example, we want to change a variable's data type from numeric to factor. Let's consider the variable "cyl" in the "mtcars" dataset as an example. Here's the code:

data <- mtcars
variable_to_convert <- "cyl"

if (variable_to_convert %in% names(data)) {
  data[[variable_to_convert]] <- as.factor(data[[variable_to_convert]])
  print(paste(variable_to_convert, "has been converted to a factor"))
} else {
  print(paste(variable_to_convert, "does not exist in the dataset"))
}

In this example, if the variable "cyl" exists in the "mtcars" dataset, it will be converted to a factor. The message "cyl has been converted to a factor" will be printed. If the variable does not exist in the dataset, the message "cyl does not exist in the dataset" will be printed.

Categorizing Individuals into Age Groups

In this example, we categorize individuals into different age groups based on their age. Let's define the age groups as follows: children (age less than 12), teenagers (age 12-19), young adults (age 20-35), adults (age 36-60), and seniors (age 61 and above). Here's the code:

age <- 34

if (age < 12) {
  print("You belong to the children age group")
} else if (age <= 19) {
  print("You belong to the teenagers age group")
} else if (age <= 35) {
  print("You belong to the young adults age group")
} else if (age <= 60) {
  print("You belong to the adults age group")
} else {
  print("You belong to the seniors age group")
}

In this example, if the age is less than 12, "You belong to the children age group" will be printed. If the age is between 12 and 19 (inclusive), "You belong to the teenagers age group" will be printed, and so on.



Abu Ubaidah al-Jarrah, known for his unwavering dedication and loyalty to Islam, was a prominent figure among the companions of Prophet Muhammad (peace be upon him). He was known as the epitome of trustworthiness and served as the guardian of the Muslim Ummah. His commitment to the faith and his remarkable achievements in the name of Islam left a lasting legacy.


Rise to Prominence

Abu Ubaidah earned the title of "Aminul Ummah" (the Trustworthy of the Ummah) through his unwavering loyalty and dedication to the cause of Islam. Even the esteemed companion Umar Bin Khattab expressed his desire to appoint Abu Ubaidah as the caliph, recognizing his exceptional qualities as a leader.

Conquest of Syria

Abu Ubaidah played a crucial role in the conquest of Syria, opening the doors to victory for the Muslims. He led the Muslim forces to liberate various regions, including Palestine, from the oppressive rule of the Byzantine Empire. His strategic brilliance and unwavering commitment to the cause of Islam were instrumental in these conquests.

The Ultimate Sacrifice

Abu Ubaidah's dedication to Islam was unwavering until his last breath. When the plague of Amwas struck, Umar called for his return, but Abu Ubaidah chose to stay with his troops, displaying his unwavering commitment to his fellow soldiers and the cause of Islam. He believed that even if he didn't die in battle, he would still attain paradise due to the guaranteed reward promised by the Prophet Muhammad (peace be upon him) for his service.

The Misunderstanding of Jihad

Unfortunately, the understanding of jihad has been distorted in recent times. Some individuals advocate for peaceful activities, such as da'wah (spreading the message of Islam), while discouraging or devaluing the importance of armed resistance against oppression. This distorted version of Islam fails to grasp the true essence of the faith as understood by Abu Ubaidah and the early companions.

True Understanding of Islam

The true understanding of Islam lies in the Quran, which is meant to be read and understood, not merely recited without comprehending its meaning. Islam is not just about seeking personal benefits or performing rituals; it is about defending and upholding the principles of justice, even if it means sacrificing one's life for the cause. Abu Ubaidah's unwavering commitment to Islam serves as a reminder of the true spirit of the faith.

Support for Palestine

The struggle of the people of Palestine against Israeli oppression is a testament to their unwavering faith and resilience. Despite limited resources and constant siege, the people of Gaza continue to resist and defend their land. Their determination and courage should inspire every Muslim to stand up for justice and support their cause.

Conclusion

Abu Ubaidah al-Jarrah's legacy is a shining example of true dedication and unwavering commitment to Islam. His remarkable achievements in the name of Islam and his selfless sacrifice serve as a source of inspiration for Muslims around the world. It is crucial for Muslims to understand the true essence of Islam and strive to uphold its principles, just as Abu Ubaidah and the early companions did.

bottom of page