x <- 60
y <- 50
if(x>y)
{
print("x is greater than y")
}
x <- 30
y <- 50
# the 'else' clause is selected...
# ...when x<=y
# ...when x>y is FALSE
if(x>y) {
print("x is greater than y")
} else {
print("x is NOT greater than y")
}
val <- 2*2
x <- switch(val,
"first",
"second",
"third",
10+9,
"fifth"
)
print(x)
?switch
x <- c(2,3,6,2,12,6,8,3,2)
operation = "median"
result <- switch(operation,
mean = mean(x),
median = median(x),
variance = var(x),
"not allowed") # default value
print(result)
v <- c("Hello","folks!")
cnt <- 2
repeat {
print(v)
cnt <- cnt+1
if(cnt > 5) {
break
}
}
# after the break it goes here!
print(cnt)
v <- c("Hello","while")
cnt <- 2
while (cnt < 7) {
print(v)
cnt = cnt + 1
}
print(cnt)
# we can interrupt the while earlier by using 'break'
cnt <- 2
while (cnt < 7) {
print(v)
cnt = cnt + 1
if(cnt == 4)
break
}
print(cnt)
v <- 1:10
for (i in v) {
print(i)
print(2*i)
}
list_of_elements <- 1:10
for (element in list_of_elements)
{
print(element*2)
}
list_of_elements <- 1:10
for (element in list_of_elements)
{
print(element*2)
if (element == 5)
break
}
list_of_elements <- 1:10
for (element in list_of_elements)
{
# this part is always executed
print(element)
if(element > 5)
next
# this part will be skipped by using 'next' (i.e., continue)
print(element*2)
}
Given the following vector of numbers, define a code to insert the even elements in the 'even' vector, the odd elements in the 'odd' vector and skip all the zeros. hint: the operator '%%' returns the reminder of a division.
odd = c()
even = c()
el_vector = c(1,2,3,4,0,10,2,0,4,6,7,8,9,0,1)