2. • The 'ifelse()' function is the alternative and
shorthand form of the R if-else statement
• All of the vector values are taken as an
argument at once rather than taking
individual values as an argument multiple
times.
Ifelse () function
3. The syntax of the ifelse() function is:
ifelse(test_expression, x, y)
The output vector has the element
● x if the output of
the test_expression is TRUE.
● If the output is FALSE, then the element
in the output vector will be y.
Syntax…
7. Lapply() function…
#create sample data
names <- c(“Rani”, “Vani”,”Ravi”, “Mala”,”Kala”)
print( “original data:”)
names
# apply lapply() function
print(“data after lapply():”)
lapply(names, toupper)
Output
[1] “Original data”
‘Rani’ ‘Vani’ ‘Ravi’ ‘Mala’ ‘Kala’
[1] “ data after lapply ():”
1. ‘Rani’
2. ‘Vani’
3. ‘Ravi’
4. ‘Mala’
5. ‘Kala’
8. seq() Function
It takes the length and difference between values as optional argument.
Syntax:
seq(from, to, by, length.out)
from: Starting element of the sequence
to: Ending element of the sequence
by: Difference between the elements
length.out: Maximum length of the vector
Example
vec1 <- seq(10, 1, by = -2)
vec2 <- seq(10, 1, length.out = 4)
print(vec1)
print(vec2)
Output:
[1] 10 8 6 4 2 [1] 10 7 4 1