0% found this document useful (0 votes)
21 views8 pages

R and Python Data Analysis Techniques

The document outlines a programming assignment using R and Python, focusing on data manipulation and visualization tasks with the iris and heart disease datasets. It includes steps for selecting specific columns, filtering data based on conditions, creating new calculated columns, and visualizing data distributions. The assignment emphasizes the use of libraries such as dplyr in R and pandas, matplotlib, and seaborn in Python.

Uploaded by

archa
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)
21 views8 pages

R and Python Data Analysis Techniques

The document outlines a programming assignment using R and Python, focusing on data manipulation and visualization tasks with the iris and heart disease datasets. It includes steps for selecting specific columns, filtering data based on conditions, creating new calculated columns, and visualizing data distributions. The assignment emphasizes the use of libraries such as dplyr in R and pandas, matplotlib, and seaborn in Python.

Uploaded by

archa
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

Programming with R and Python:JN24

Assignment-4 (Descriptive)

1)Question 1
 Select petal length and sepal length from the given dataset

We are using the iris dataset and the dplyr package for data
manipulation.

# Load required library

library(dplyr)

# Load the iris dataset

data(iris)

# Select petal length and sepal length

selected_data <- iris %>%

select([Link], [Link])

# View the selected data

head(selected_data)

2) Filter the data for petal length > 5.5 and select relevant classes

# Filter data for petal length > 5.5

filtered_data <- iris %>%

filter([Link] > 5.5) %>%

select(Species, [Link])
# View the filtered data

head(filtered_data)

3) Create a new column ‘flower area’ to represent the total area occupied by
the flower petals.

flower area=[Link]×[Link]

# Calculate flower area

iris_with_area <- iris %>%

mutate(flower_area = [Link] * [Link])

# View the data with the new column

head(iris_with_area)

 Selection of library

 dplyr: We are using the dplyr package for data manipulation task.

 Choice of function 1

 filter(): We are using the filter() function to filter rows based on a


condition ([Link] > 5.5).

 Choice of function 2

 mutate(): We are using the mutate() function to create a new column


(flower_area) based on the calculated petal area ([Link] *
[Link]).

Full Code

# Load necessary libraries


library(dplyr)

# Load the iris dataset

data(iris)

# Select petal length and sepal length

selected_data <- iris %>%

select([Link], [Link])

# Filter data for flowers with petal length greater than 5.5 units

filtered_data <- selected_data %>%

filter([Link] > 5.5)

# Create a new column 'flower area'

# Assuming 'flower area' is the product of petal length and sepal length

filtered_data <- filtered_data %>%

mutate(flower_area = [Link] * [Link])

# Display the resulting data

print(filtered_data)
2)Question 2

import pandas as pd

import [Link] as plt

import seaborn as sns

# Into a Pandas DataFrame, load the data from [Link]


df = pd.read_csv('spotify_data.csv')

# Plotting using seaborn

[Link](figsize=(10, 6))

# Use seaborn's histplot to create a histogram with density overlay

[Link](data=df, x='song_duration', hue='song_genre', kde=True,


stat='density', palette='Set2', linewidth=0)

# Customize labels and title

[Link]('Song Duration (seconds)')

[Link]('Density')

[Link]('Distribution of Song Duration by Genre')

# Show legend

[Link](title='Genre')

# Display the plot

[Link]()

Output
3)Question 3

 Read the CSV file into a dataframe ‘hdf’.

 Display the summary statistics for the column ‘cholestrol’.

 Create a new column BMI, while also adding the required variables.

Code

import pandas as pd

import numpy as np

# Read the CSV file into a dataframe 'hdf'

file_path_hd = '[Link]'

hdf = pd.read_csv(file_path_hd)

# Display summary statistics for the 'chol' column

chol_summary = hdf['chol'].describe()

print("Summary statistics for 'chol':")


print(chol_summary)

# Add hypothetical columns for weight (kg) and height (m)

[Link](0) # For reproducibility

hdf['weight'] = [Link](50, 100, size=len(hdf))

hdf['height'] = [Link](1.5, 2.0, size=len(hdf))

# Calculate BMI

hdf['BMI'] = hdf['weight'] / (hdf['height'] ** 2)

# Display the first few rows of the modified dataframe

print("\nFirst few rows of the modified dataframe:")

print([Link]())

You might also like