0% found this document useful (0 votes)
34 views12 pages

ML File

Data can be

Uploaded by

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

ML File

Data can be

Uploaded by

hdofficial2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Practical No.

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

The Help Facility


R has a comprehensive help system to assist you with functions, packages, and more.
1.‘?’ Operator: In R, you can use the ? operator to access documentation for functions and packages. Simply
type ? followed by the function or package name you want to learn more about.
2. ‘help()’ Function: You can also use the help() function to access documentation. For
3. Example Usage: Many help pages include examples of how to use a function. These examples are helpful
for understanding how a function works.
4. Package Help: To access help for a specific package, use help(package = "package_name"). Replace
package_name with the name of the package you want to learn about.
In R Studio, you can access the help facility in the "Help" pane. When you search for a function or package,
it will provide you with information and examples in a user-friendly format.
Practical No.2
Q2. Data Structures: Vectors, Matrices, List and Data Frames.

1. Vector

A vector is simply a list of items that are of the same type.

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 is a two dimensional data set with columns and rows.

A column is a vertical representation of data, while a row is a horizontal representation of data.

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.

To create a list, use the list( ) function:

Code:

# List of characters/strings

thislist <- list("apple", "banana", "cherry")

# Print the list

thislist

Output:

4. Data Frames

Data Frames are data displayed in a format as a table.

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.

Use the data.frame() function to create a data frame:

Code:

# Create a data frame


Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
# Print the data frame
Data_Frame
Output:
Practical No.3
Q3. Reading Data into R from various Data Sources. Merging Data across Data Sources.
Reading data in R various Data Sources
In R, you can read data from various data sources, including text files, databases, web services, and more.
Here are some common methods and packages for reading data into R from different sources:
1.Reading Data from Text Files:
Code: # Read data from a CSV file
data <- read.csv("data.csv")
# Read data from a tab-separated file
data <- read.table("data.tsv", sep = "\t")
# Read data from a plain text file
text <- readLines("data.txt")
Output:

2.Reading Excel Files: Code: library(readxl)


# Read data from an Excel file
data <- read_excel("data.xlsx")
Output:

3. Reading Data from Databases:


Code: library(DBI)
library(RSQLite)
# Establish a database connection
con <- dbConnect(RSQLite::SQLite(), "mydatabase.db")
# Execute a query to fetch data
data <- dbGetQuery(con, "SELECT * FROM mytable")
# Close the database connection
dbDisconnect(con)
Output:

4.Reading Data from Web Services:


Code: library(httr)
library(jsonlite)
# Make an HTTP GET request
response <- GET("https://api.example.com/data")
# Parse JSON response
data <- fromJSON(content(response, "text"))
Merging Data across Data Sources
Code: # Establish a connection to our SQLite database
con <- dbConnect(RSQLite::SQLite(), "./data/students.db")

# Write our query using SQL syntax


query <- "SELECT Students.stuid, Majors.majorname
FROM Students INNER JOIN Majors
ON Students.majorid = Majors.majorid"

# 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:

GLM (Generalized linear model)


Generalized Linear Models (GLMs) in R are a class of regression models that support non-normal
distributions. They are used in a wide range of statistical models, including: Linear regression, Logistic
regression, Poisson regression.
Code:
# Sample data
count_data <- data.frame(x = 1:5, y = c(2, 3, 4, 5, 6))
# Fit a Poisson GLM (count data)
glm_model <- glm(y ~ x, data = count_data, family = poisson)
# Print the model summary
summary(glm_model)
Output:
Practical No.5
Q5. Write your own function in R.

A function is a block of code which only runs when it is called.

You can pass data, known as parameters, into a function.

A function can return data as a result.

Code:

king <- function() {

print("Hello World!")

king() # call the function named king

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:

You might also like