R-Program
R-Program
print(dataframe1)
Access Data Frame Column
There are different ways to extract columns from a data frame. We can use [ ],
[[ ]], or $ to access specific column of a data frame in R
Combine Data Frames
We can use the array() function to create an array, and the dim parameter to
specify the dimensions:
Arrays are the R data objects which can store data in more than two dimensions.
For example − If we create an array of dimension (2, 3, 4) then it creates 4
rectangular matrices each with 2 rows and 3 columns. Arrays can store only data
type.
R Arrays
R Array Syntax
There is the following syntax of R arrays:
1.In the first step, we will create two vectors of different lengths.
2.Once our vectors are created, we take these vectors as inputs
to the array.
print(array1)
Access Array Elements
We use the vector index operator [ ] to access specific elements of an array in R.
print(array1)
print(array1)
Output
[1] Programiz Pro
message1 <- "Programiz"
message2 <- "Pro"
We use the == operator to compare two strings. If two strings are equal, the operator returns
TRUE. Otherwise, it returns FALSE
Syntax:
switch(Expression, "Option 1", "Option 2", "Option
3" ................................."OptionN")
Example: Print Department names using Switch
2.Looping Statements
While Loop:
Description: The While loop executes the same code again and again until a stop condition is met.
Syntax:
Example:
Out Put:
while(condition)
{ i <- 1 [1] 1
Statement while (i < 6) [1] 2
} [1] 3
{ [1] 4
[1] 5
print(i)
i <- i + 1
}
For Loop in R
[1] 1
[1] "apple" [1] 2
[1] "banana" [1] 3
[1] "cherry" [1] 4
repeat loop
1. A repeat loop is used to iterate over a block of code multiple number of times.
2. There is test expression in a repeat loop to end or exit the loop
3. use the break function to exit the loop. Failing to do so will result into an
infinite loop.
Syntax:
repeat
{
Commands
If(condition)
Break
}
repeat loop
x=1
# Repeat loop
repeat { Output:
print(x) [1] 1
[1] 2
# Break statement to terminate if x > 4 [1] 3
if (x > 4) { [1] 4
break [1] 5
}
# Increment x by 1
x=x+1
}
3.Jump Statements
Break :
Terminates the loop statement and transfers execution to the statement
immediately following the loop.
Syntax: break
Example :print numbers till 5
next Statement in R
if (test_condition)
{
next
}
In this example, the for loop will iterate for each element in x; however, when it
gets to the element that equals 3 it will skip the for loop execution of printing the
element and simply jump to the next iteration.
x <- 1:5
for (i in x)
{
if (i == 3)
{
next
}
print(i)
}
## [1] 1
## [1] 2
## [1] 4
## [1] 5
R - Functions
Syntax:
function_name <- function(arg_1, arg_2, ...)
{
Function body
}
Components of Functions
Function Types
Creating a Function Call a Function
To create a function, use the function() keyword:
To call a function, use the function name
followed by parenthesis, like my_function():
Example:
Example
my_function <- function()
{ my_function <- function()
print("Hello World!") {
} print("Hello World!")
}
apply() takes Data frame or matrix as an input and gives output in vector, list or
array. Apply function in R is primarily used to avoid explicit uses of loop
constructs. It is the most basic of all collections can be used over a matrice.
This function takes 3 arguments:
Output:
lapply()
Definition: Loop over a list and evaluate a function on each element
Syntax:
lapply(X, FUN)
Arguments:
-X: A vector or an object
-FUN: Function applied to each element
of x
sapply()
Syntax:
sapply(X, FUN)
Arguments:
-X: A vector or an object
-FUN: Function applied to each element
of x
sapply()
We use the tapply() function for calculating summary statistics (such as mean, median,
min, max, sum, etc.) for different factors (i.e., categories). It has the following syntax: