ML File
ML File
1
Q1. Introduction to R and R Studio. Using the help facility.
R is an open-source programming language that is widely used as a statistical software and data analysis tool.
R generally comes with the Command-line interface. R is available across widely used platforms like
Windows, Linux, and macOS. Also, the R programming language is the latest cutting-edge tool.
RStudio is an open-source tool that provides an IDE for working with the R programming language. R is a
popular statistical analysis and machine-learning package that includes tests, models, analyses, and graphics.
RStudio is used in data analysis to:
1. Import, access, transform, explore, plot, and model data
2.Make predictions on data
RStudio is particularly beneficial for:
1. Beginners
2. Those working on complex projects with multiple files and packages
1. Vector
To combine the list of items to a vector, use the c( ) function and separate the items by a comma.
In the example below, we create a vector variable called fruits that combine strings:
Code:
#Vector of characters/strings
fruits <- c("banana", "apple", "orange")
# Print fruits
fruits
Output:
2. Matrices
A matrix can be created with the matrix() function. Specify the nrow and ncol parameters to get the amount
of rows and columns:
Code:
# Create a matrix
thismatrix <- matrix(c(1,2,3,4,5,6), nrow = 3, ncol = 2)
# Print the matrix
thismatrix
Output:
3. List
A list in R can contain many different data types inside it. A list is a collection of data which is ordered and
changeable.
Code:
# List of characters/strings
thislist
Output:
4. Data Frames
Data Frames can have different types of data inside it. While the first column can be character, the second
and third can be numeric or logical. However, each column should have the same type of data.
Code:
# Execute the query, fetch the results, and store them in a data frame
students <- dbFetch(dbSendQuery(con, query))
Output:
Practical No.4
Q4. Statistical Modeling functions: LM (Linear Regression Model) and GLM (Generalized linear model).
LM (Linear Regression Model)
The lm() function creates a linear regression model in R. This function takes an R formula Y ~ X where Y is
the outcome variable and X is the predictor variable. To create a multiple linear regression model in R, add
additional predictor variables using + .
Code:
# Sample data
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 6, 8, 10)
# Fit a linear regression model
lm_model <- lm(y ~ x)
# Print the model summary
summary(lm_model)
Output:
Code:
print("Hello World!")
Output:
Practical No.6
Q6. Writing your own function in R
Code:
my_function <- function(fname) {
paste(fname, "Griffin")
}
my_function("Peter")
my_function("Lois")
my_function("Stewie")
Output:
Practical No.7
Q7. Iterating with R: Logic and Flow Control.
Iterating gained in R with the help of Loops
Loops
Loops can execute a block of code as long as a specified condition is reached.
Loops are handy because they save time, reduce errors, and they make code more readable.
R has three loop commands:
• While Loops
• For Loops
• Nested Loop
While Loop
With the while loop we can execute a set of statements as long as a condition is TRUE:
Code:
i <- 0
while (i < 6) {
i <- i + 1
if (i == 3) {
next
}
print(i)
}
Output:
For Loop
A for loop is used for iterating over a sequence:
Code:
fruits <- list("apple", "banana", "cherry", “mango”, “guava”, “pineapple”)
for (x in fruits) {
if (x == "pineapple") {
break
}
print(x)
}
Output:
Nested Loop
It is also possible to place a loop inside another loop. This is called a nested loop:
Code:
adj <- list("red", "big", "tasty")
fruits <- list("apple", "banana", "cherry")
for (x in adj) {
for (y in fruits) {
print(paste(x, y))
}
}
Output: