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

R-Lab p-4,2,1

Uploaded by

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

R-Lab p-4,2,1

Uploaded by

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

Program -4 – VISUALIZATIONS

a.Find the data distributions using box and scatter plot.

Install.packages(“ggplot2”)

Library(ggplot2)

Input <- mtcars[,c('mpg','cyl')]

Input

Boxplot(mpg ~ cyl, data = mtcars, xlab = "number of cylinders", ylab = "miles per gallon", main =
"mileage data")

Dev.off()

Output :- mpg cyl

Mazda rx4

21.0 6

Mazda rx4 wag 21.0 6

Datsun 710

22.8 4

Hornet 4

drive 21.4 6

Hornet

sportabout

18.7 8

Valiant 18.1

6
b. Find the outliers using plot.

v=c(50,75,100,125,150,175,200)

boxplot(v)

c. Plot the histogram, bar chart and pie chart on sample data.

Histogram
library(graphics)

v <- c(9,13,21,8,36,22,12,41,31,33,19)

hist(v,xlab = "Weight",col = "blue",border = "green")

dev.off()

Output:

Bar chart

library(graphics)

H <- c(7,12,28,3,41)

M <- c("Jan","Feb","Mar","Apr","May")

barplot(H,names.arg = M,xlab = "Month",ylab = "Revenue",col = "blue",main = "Revenue chart",border =


"red")

dev.off()

output:
Pie Chart

library(graphics)

x <- c(21, 62, 10, 53)

labels<- c("London", "NewYork", "Singapore", "Mumbai")

pie(x,labels)

dev.off()

output:
Program-2: DESCRIPTIVE STATISTICS IN R
a. Write an R script to find basic descriptive statistics using summary

>mtcars

> summary(mtcars)

>str(mtcars)

>quantile(mtcars$mpg)

>cars

> summary(cars)

> class(cars)

Output:

[1] "data.frame"

>dim(cars)

Output:

[1] 50 2

> str(cars)

>quantile(cars$speed)

b. Write an R script to find subset of dataset by using subset (), aggregate ()


functions on iris dataset.
>aggregate(. ~ Species, data = iris, mean)

>subset(iris,iris$Sepal.Length==5.0)

Program-1:
c.Write Program make a simple calculator that can add, subtract, multiply and
divide using functions.
add <- function(x, y) {

return(x + y) }

subtract <- function(x, y) {

return(x - y) }

multiply <- function(x, y) {


return(x * y) }

divide <- function(x, y) {

return(x / y) }

print("Select operation.")

print("1.Add")

print("2.Subtract")

print("3.Multiply")

print("4.Divide")

choice = as.integer(readline(prompt="Enter choice[1/2/3/4]: "))

num1 = as.integer(readline(prompt="Enter first number: "))

num2 = as.integer(readline(prompt="Enter second number: "))

operator <- switch(choice,"+","-","*","/")

result <- switch(choice, add(num1, num2), subtract(num1, num2), multiply(num1, num2),


divide(num1, num2))

print(paste(num1, operator, num2, "=", result))


Program -3: READING A CSV FILE:

data <- read.csv("input.csv")

print(data)

Analyzing the CSV File:

data <- read.csv("input.csv")

print(is.data.frame(data))

print(ncol(data))

print(nrow(data))

Get the maximum salary:

# Create a data frame.

data <- read.csv("input.csv")

# Get the max salary from data frame.

sal <- max(data$salary)

print(sal)

Get the details of the person with max salary

# Create a data frame.

data <- read.csv("input.csv")

# Get the max salary from data frame.

sal <- max(data$salary)

# Get the person detail having max salary.

retval <- subset(data, salary == max(salary))

print(retval)

Get all the people working in IT department

# Create a data frame.

data <- read.csv("input.csv")

retval <- subset( data, dept == "IT")

print(retval)
Get the persons in IT department whose salary is greater than 600

# Create a data frame.

data <- read.csv("input.csv")

info <- subset(data, salary > 600 & dept == "IT")

print(info)

Get the people who joined on or after 2014

# Create a data frame.

data <- read.csv("input.csv")

retval <- subset(data, as.Date(start_date) > as.Date("2014-01-01"))

print(retval)

Writing into a CSV File

# Create a data frame.

data <- read.csv("input.csv")

retval <- subset(data, as.Date(start_date) > as.Date("2014-01-01"))

# Write filtered data into a new file.

write.csv(retval,"output.csv")

newdata <- read.csv("output.csv")

print(newdata)

Here the column X comes from the data set newper. This can be dropped using

additional parameters while writing the file.

# Create a data frame.

data <- read.csv("input.csv")

retval <- subset(data, as.Date(start_date) > as.Date("2014-01-01"))

# Write filtered data into a new file.

write.csv(retval,"output.csv", row.names = FALSE)

newdata <- read.csv("output.csv")

print(newdata)
b. Reading Excel data sheet in R.

Verify and Load the "xlsx" Package

# Verify the package is installed.

any(grepl("xlsx",installed.packages()))

# Load the library into R workspace.

library("xlsx")

Input as xlsx File

Reading the Excel File

# Read the first worksheet in the file input.xlsx.

Data <- read.xlsx(“input.xlsx”, sheetIndex = 1)

Print(data)

Or

Install.packages(“xlsx”)

Library(“xlsx”)

Data<- read.xlsx(“input.xlsx”, sheetIndex = 1)

Data

c. Reading XML dataset in R.

install.packages(“XML”)

Input Data

<RECORDS>

<EMPLOYEE>

<ID>1</ID>

<NAME>Rick</NAME>

<SALARY>623.3</SALARY>

<STARTDATE>1/1/2012</STARTDATE>

<DEPT>IT</DEPT>
</EMPLOYEE>

<EMPLOYEE>

<ID>2</ID>

<NAME>Dan</NAME>

<SALARY>515.2</SALARY>

<STARTDATE>9/23/2013</STARTDATE>

<DEPT>Operations</DEPT>

</EMPLOYEE>

<EMPLOYEE>

<ID>3</ID>

<NAME>Michelle</NAME>

<SALARY>611</SALARY>

<STARTDATE>11/15/2014</STARTDATE>

<DEPT>IT</DEPT>

</EMPLOYEE>

<EMPLOYEE>

<ID>4</ID>

<NAME>Ryan</NAME>

<SALARY>729</SALARY>

<STARTDATE>5/11/2014</STARTDATE>

<DEPT>HR</DEPT>

</EMPLOYEE>

<EMPLOYEE>

<ID>5</ID>

<NAME>Gary</NAME>

<SALARY>843.25</SALARY>

<STARTDATE>3/27/2015</STARTDATE>

<DEPT>Finance</DEPT>

</EMPLOYEE>
<EMPLOYEE>

<ID>6</ID>

<NAME>Nina</NAME>

<SALARY>578</SALARY>

<STARTDATE>5/21/2013</STARTDATE>

<DEPT>IT</DEPT>

</EMPLOYEE>

<EMPLOYEE>

<ID>7</ID>

<NAME>Simon</NAME>

<SALARY>632.8</SALARY>

<STARTDATE>7/30/2013</STARTDATE>

<DEPT>Operations</DEPT>

</EMPLOYEE>

<EMPLOYEE>

<ID>8</ID>

<NAME>Guru</NAME>

<SALARY>722.5</SALARY>

<STARTDATE>6/17/2014</STARTDATE>

<DEPT>Finance</DEPT>

</EMPLOYEE>

</RECORDS>
Reading XML File

# Load the package required to read XML files.

Library(“XML”)

# Also load the other required package.

Library(“methods”)

# Give the input file name to the function.

Result <- xmlParse(file = “input.xml”)

# Print the result.

Print(result)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy