Filefile
Filefile
Practical File
NO. SIGN
8
Program to read XML datasets in R.
Experiment 1
Calculator in R (without using objects)
SOURCE CODE
# Without using objects (just performing operations directly)
# Addition
3+5
# Subtraction
10 - 2
# Multiplication
4*7
# Division
20 / 4
# Power (Exponentiation)
2^3
Output
Calculator in R (using objects)
SOURCE CODE
# Using objects (variables) for operands and results
num1 <- 10
num2 <- 5
# Addition
print(paste("Addition:", result_add))
# Subtraction
print(paste("Subtraction:", result_sub))
# Multiplication
print(paste("Multiplication:", result_mult))
# Division
print(paste("Division:", result_div))
# Power (Exponentiation)
print(paste("Exponentiation:", result_pow))
Output
Experiment:2
AIM:Calculator in R using Mathematical functions.
SOURCE CODE
# Function to perform basic operations
operation <- readline(prompt = "Enter operation (+, -, *, /, ^, sqrt, log, sin, cos, tan): ")
if (operation == "sqrt") {
# Sine function
# Cosine function
# Tangent function
# Exponentiation
# Addition
# Multiplication
# Division
if (num2 != 0) {
} else {
} else {
print("Invalid operation!")
calculator()
Output
Experiment:3
AIM: R Script: Calculator Application with R Objects
SOURCE CODE
# Calculator Application using R Objects
# Addition
# Multiplication
# Division
if (num2 != 0) {
} else {
Output
Experiment 4
AIM: Write R script to find basic descriptive statistics using
summary, str, quartile functions on mtcars & cars datasets.
SOURCE CODE
# Load the datasets
data(mtcars)
data(cars)
print(summary(mtcars))
str(mtcars)
str(cars)
SOURCE CODE
# Load the iris dataset
data(iris)
# Subset the data to include only the rows where species is 'setosa'
print(head(setosa_data))
# Subset the data to include only rows where Sepal.Length is greater than 5
print(head(sepal_length_subset))
# Subset the data to include only columns Sepal.Length, Sepal.Width, and Petal.Length
print(head(subset_columns))
# ---- Using aggregate() function ----
# Aggregate data by Species, calculating the mean of Sepal.Length, Sepal.Width, Petal.Length, and
Petal.Width
print(aggregated_data)
# Aggregate the data to calculate the sum of Sepal.Length and Petal.Width by Species
print(aggregate_sum)
# Aggregate the data to find the count of observations for each Species
print(aggregate_count)
Output
Experiment:6
AIM: Write a program in R for reading different
types of datasets(.csv)from web and disk and writing
in file in specific disk location.
SOURCE CODE
R Script for Reading and Writing Datasets
We can use read.csv(), read.table(), or read.delim() for reading datasets from the web.
For this example, we'll use read.csv() to load a .csv file from the web.
For reading data from the local disk, we can use read.csv(), read.table(), or readRDS() (for
R-specific files). Here, we'll demonstrate reading .csv and .txt files.
For writing data, we can use write.csv(), write.table(), or saveRDS() depending on the
format we want to save.
R Script
# ---- 2. Read Dataset from the Disk (CSV and TXT Files) ----
print(head(local_csv_data))
print(head(local_txt_data))
# Writing to TXT
SOURCE CODE
To read Excel files in R, we can use a package like readxl or openxlsx. The most commonly used
package for reading Excel files in R is readxl because it is simple to use and does not require any
external dependencies (like Java).
print(head(excel_data))
cat("XML Content:\n")
print(xml_data)
# <person>
# <name>John Doe</name>
# <age>30</age>
# <address>New York</address>
# </person>
cat("\nExtracted Data:\n")
# <person id="123">
# <name>John Doe</name>
# <age>30</age>
# </person>
# <people>
# <person>
# <name>John</name>
# <age>30</age>
# <address>New York</address>
# </person>
# <person>
# <name>Jane</name>
# <age>25</age>
# <address>Los Angeles</address>
# </person>
# </people>
Name = person_names,
Age = person_ages,
Address = person_addresses
print(people_data)
6. Working with XML Namespaces
# Example XML with namespaces:
# <ns:person xmlns:ns="http://www.example.com">
# <ns:name>John Doe</ns:name>
# <ns:age>30</ns:age>
# </ns:person>
ns <- xml_ns(xml_data)
xml_write(root, output_xml_path)
SOURCE CODE
Step 1: Install and Load Necessary Libraries
install.packages("ggplot2")
library(ggplot2)
data <- data.frame(value = rnorm(100)) # 100 random numbers from a normal distribution
SOURCE CODE
# Step 1: Create a sample dataset
data <- c(rnorm(100), 10, 12, 14) # Normal distribution + some outliers