0% found this document useful (0 votes)
7 views4 pages

R Control Structures and Examples

The document provides examples of conditional and iterative structures in R programming, including if-else statements and for loops. It demonstrates how to categorize ages, evaluate grades, and filter positive numbers from vectors. Additionally, it includes exercises for modifying loops and creating data frames with conditional columns.

Uploaded by

cuadradomar472
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

R Control Structures and Examples

The document provides examples of conditional and iterative structures in R programming, including if-else statements and for loops. It demonstrates how to categorize ages, evaluate grades, and filter positive numbers from vectors. Additionally, it includes exercises for modifying loops and creating data frames with conditional columns.

Uploaded by

cuadradomar472
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

### PRACTICAL CASES Unit 3.

R Control Structures #####


######## CONDITIONAL STRUCTURES #########
# Conditional structure
edad <- 25
if (edad < 18) {
categoria <- "Minor"
} else if (edad >= 18 & edad < 65) {
categoria <- "Adult"
} else {
categoria <- "Senior"
}
categoria
# We increase the ranges
edad <- 16
if (edad < 13) {
categoria <- "Child"
} else if (edad >= 13 & edad < 18) {
categoria <- "Teenager"
} else if (edad >= 18 & edad < 65) {
categoria <- "Adult"
} else {
categoria <- "Senior"
}
categoria
###### EXERCISES ######
### 1. Break down the second else if into 2 levels: 18 - 35: young adult and 35 - 65: adult.
edad <- 16
if (edad < 0) {
categoria <- "Age not valid"
}else if ( edad >= 0 & edad < 13) {
categoria <- “Child”
} else if (edad >= 13 & edad < 18) {
categoria <- "Teenager"
} else if (edad >= 18 & edad < 35) {
categoria <- “Young adult
} else if (edad >= 35 & edad < 65) {
categoria <- "Adult"
} else {
categoria <- "Senior"
}
categoria
### 4. Create an if conditional structure with several else if branches to evaluate the value of
grade <- 5
if (grade < 5){
categoria <- “fail”
}else if ( grade >= 5 & grade < 7 )
categoria <- “ pass”
}else if ( grade >= 7 & grade < 9){
categoria <- “very good”
}else if (grade >= 9 ){
categoria <- “excellent”
}
#####################################
######## ITERATIVE STRUCTURES #####

# Loop to check if there are negative numbers


v1 = c(1,-2,6,-4,3, -7, 9)
for (i in v1) {
if (i > 0) {
print(i)
}
}
# We store the positive numbers in another vector
v1 = c(1,-2,6,-4,3, -7, 9)
v2 <- c()
for (i in v1) {
if (i > 0) {
v2 <- c(v2, i)
}
}
v2
# Vector of numbers from 1 to 10
numbers <- c(1:10)
if (n %% 2 == 0) {
print(paste(n, "is an even number"))
} else {
print(paste(n, "is an odd number"))
}
}

###### EXERCISES ######


### 1. Modify the last loop to generate a vector called numbers_pos that stores the positive
values of the vector numbers.
numbers_pos <- c()
for (i in v) {
if ( i > 0) {
numbers_pos <- c(numbers_pos,i)
}
}
numbers_pos
### 2. Use the length function to check the length of the vector numbers_pos
length(numbers_pos)
# Let's go through the ages with a for loop and create a new column that indicates if each
person is over 30 years old

df1 <- [Link](


nombre = c("Ana", "Luis", "Marta"),
edad = c(28, 35, 42),
ciudad = c("Salamanca", "Valencia", "Sevilla")
)
mayor_30 <- c()
for (e in df1$edad) {
if (e > 30) {
mayor_30 <- c(mayor_30, "Sí")
} else {
mayor_30 <- c(mayor_30, "No")
}
}
df1$mayor_30 <- mayor_30
df1

####################################
######## WHILE AND REPEAT LOOPS ####
####################################

# Create a vector with values from 1 to 10


x <- 1
v <- c() # initialize loop
while (x <= 10) {
print(paste("The value of x is", x))
v <- c(v, x) # add the value
x <- x + 1 # Important: update the variable, otherwise the loop would be infinite
}
v
###### EXERCISES ######
### 1. Starting from the previous case, modify the code to store in vector v only the values
that are multiples of 3. End the loop at value 30.
v3 <- c()
for ( x in 1:30) {
if ( x %% 3 == 0 & x <= 30) {
v3 <- c(v3,x)
}
x <- x +1
}
v3

You might also like