Assignment

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

University Institute of Engineering

Department of Computer Science & Engineering

NAME: Md Sharjil Alam


UID: 21BCS2854

Assignment 2
Question 1: Make a basic histogram using the age data from the Titanic data set using
ggplot.

Answer:

Code:
titanic_data %>%
ggplot(aes(x = Age, fill = Survived)) +
geom_histogram() +
theme_classic() + theme(
plot.title = element_text(family = "Times New Roman", hjust = 0.5), axis.text =
element_text(family = "Times New Roman",face = "bold"), axis.title =
element_text(family = "Times New Roman", face = "bold"),
legend.title = element_blank(),
legend.text = element_text(family = "Times New Roman")

)+
labs(title = "Survival rates by Age")

DATA SCIENCE USING R


University Institute of Engineering
Department of Computer Science & Engineering
Output:

DATA SCIENCE USING R


University Institute of Engineering
Department of Computer Science & Engineering

Question 3: The ‘cars’ data set gives the speed of cars and the distances
taken to stop. Note that the data were recorded in the 1920s. Plot the
‘cars’ data set as a scatter plot.

Answer:
Code:

library(ggplot2)

ggplot(cars, mapping = aes(x = speed, y = dist)) + geom_point(mapping


= aes(color = dist > 70)) + scale_color_manual(values = c("green",
"black")) + geom_smooth()

Output:

DATA SCIENCE USING R


University Institute of Engineering
Department of Computer Science & Engineering

Question 5: Explain the steps of reading and writing into a CSV file.

Answer:

Writing to CSV files in R:-

To write to csv file write.csv() function is used.

Syntax:
write.csv(data, path) Parameter:
data: data to be added to csv
path: pathname of the file

Approach:
Create a DataFrame
Pass required values to the function
Write to file
Save the file

Reading to CSV files in R:-

To read a csv file read.csv() function is used.


Syntax:
read.csv(file, header, sep, dec) Parameters:
file: the path to the file containing the data to be imported into R.
header: logical value. If TRUE, read.csv() assumes that your file
has a row, so row 1 is the name of each column. If that’s not the case,
you can add the argument header = FALSE.
sep: the field separator character.
dec: the character used in the file for decimal points.

DATA SCIENCE USING R

You might also like