0% found this document useful (0 votes)
32 views6 pages

R Pgms 30

The document describes an R program that performs operations on data frames containing item information. It creates a data frame with item codes, categories, and prices. It then subsets the data frame to retrieve items based on various criteria, such as price or category. It calculates the total price of items and merges the item data frame with another data frame containing quantity and reorder level information. The document also demonstrates creating factors and performing operations on them. Finally, it shows various analyses that can be done on the iris dataset, including summaries, histograms, scatterplots, and calculating correlations.

Uploaded by

thanvishetty2703
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)
32 views6 pages

R Pgms 30

The document describes an R program that performs operations on data frames containing item information. It creates a data frame with item codes, categories, and prices. It then subsets the data frame to retrieve items based on various criteria, such as price or category. It calculates the total price of items and merges the item data frame with another data frame containing quantity and reorder level information. The document also demonstrates creating factors and performing operations on them. Finally, it shows various analyses that can be done on the iris dataset, including summaries, histograms, scatterplots, and calculating correlations.

Uploaded by

thanvishetty2703
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/ 6

6.

Write an R program to create a Data Frame with following details and do the
following operations.

ItemCode itemCategory ItemPrice


1001 Electronics 700
1002 DesktopSupplies 300
1003 Office Supplies 350
1004 USB 400
1005 CD Drive 800

a. Subset the Data frame and display the details of only those items whose price is
greater than or equal to 350.
b. Subset the Data frame and display only the items where the category is either
“Office Supplies” or “Desktop Supplies”
c. Subset the Data frame and display the items where the Itemprice between 300 and
700
d. Compute the sum of all ItemPrice
e. Create another Data Frame called “item-details” with three different fields
itemCode, ItemQtyonHand and ItemReorderLvl and merge the two frames.
# Create a data frame item_data
item_data <- data.frame(ItemCode = c("1001", "1002", "1003", "1004", "1005"),
ItemCategory = c("Electronics", "Desktop Supplies", "Office Supplies", "USB",
"CD Drive"),ItemPrice = c(700, 300, 350, 400, 800))
# Print the data frame
print(item_data)
# Subset the data frame to get items with price >= 350
Print(“a. Subset the Data frame and display the details of only those items whose
price is greater than or equal to 350. ”)
subset_data_GT <- subset(item_data, ItemPrice >= 350)
# Print the subsetted data frame
print(subset_data_GT)
print(“b. Subset the Data frame and display only the items where the category is
either Office Supplies or Desktop Supplies”)
subset_data_DO<-subset(item_data,ItemCategory=="DesktopSupplies"|
ItemCategory=="Office Supplies")
# Print the subsetted data frame
print(subset_data_DO)
print(“c. Subset the Data frame and display the items where the Itemprice between
300 and 700 ”)
subset_data_Between <- subset(item_data, ItemPrice >= 300 & ItemPrice <= 700)
# Print the subsetted data frame
print(subset_data_Between)
print(“d. Compute the sum of all ItemPrice ”)
# Calculate the sum of ItemPrice
total_price <- sum(item_data$ItemPrice)
# Print the sum
print(total_price)

# Create the item-details data frame


Print(“e. Create another Data Frame called “item-details” with three different fields
itemCode, ItemQtyonHand and ItemReorderLvl and merge the two frames.”)
item_details <- data.frame(ItemCode = c("1001", "1002", "1003", "1004", "1005"),
ItemQtyonHand = c(15, 10, 25, 8, 12), ItemReorderLvl = c(5, 8, 10, 6, 9))
# Merge the two data frames based on itemCode
merged_data <- merge(item_data, item_details, by = "ItemCode")
# Print the merged data frame
print(merged_data)

7. Create a factor marital_status with levels Married, single, divorced. Perform the
following operations on this factor
a. Check the variable is a factor
b. Access the 2nd and 4th element in the factor
c. Remove third element from the factor
d. Modify the second element of the factor
e. Add new level widowed to the factor and add the same level to the factor
marital_status .
marital_statuses <- factor(c("Married", "Single", "Divorced","Married", "Single",
"Divorced"))
marital_statuses
print("a. Check the variable is a factor")
is_factor <- is.factor(marital_statuses)
# Print the result
print(is_factor)
print("b. Access the 2nd and 4th element in the factor")
second_element <- marital_statuses[2]
fourth_element <- marital_statuses[4]
print(second_element)
print (fourth_element)
print("c. Remove third element from the factor")
removed_factor <- marital_statuses[-3]
print(removed_factor)

print("d. Modify the second element of the factor ")

marital_statuses[2] <- "Married"


print(marital_statuses)
# Adding 'Widowed' level to 'marital_factor'
print("e. Add new level widowed to the factor and add the same level to the factor
marital_status ")
marital_statuses <- factor(marital_statuses, levels = c(levels(marital_statuses),
"Widowed"))
# Print the modified factors
print(marital_statuses)
marital_statuses <- factor(c(as.character(marital_statuses),"Widowed"))
print(marital_statuses)

8. Write a R language Script for following operation on Iris Data Set


1. Load the Iris Dataset
2. View first six rows of iris dataset
3. Summarize iris dataset
4. Display number of rows and columns
5. Display column names of dataset.
6. Create histogram of values for sepal length
7. Create scatterplot of sepal width vs. sepal length
8. Create boxplot of sepal width vs. sepal length
9. Find Pearson correlation between Sepal.Length and Petal.Length
10. Create correlation matrix for dataset.

print("Write a R language Script for following operation on Iris Data Set")


print("1. Load the Iris Dataset")
data(iris)
print("2. View first six rows of iris dataset")
print(head(iris))
#or
print(iris[1:6,])
print("3. Summarize iris dataset")
#summary
summary(iris)

#row and col


print("4. Display number of rows and columns")
Dim_row_col <- dim(iris)
print(Dim_row_col)
#name
print("5. Display column names of dataset.")

Names_iris <- names(iris)


print("Names_iris)
#histogram
print("6. Create histogram of values for sepal length")

hist(iris$Sepal.Length,
col='steelblue',
main='Histogram',
xlab='Length',
ylab='Frequency')
#create scatterplot of sepal width vs. sepal length
boxplot(Sepal.Length~Species,
data=iris,
main='Sepal Length by Species',
xlab='Species',
ylab='Sepal Length',
col='steelblue',
border='black')
# Calculate the Pearson correlation between Sepal.Length and Petal.Length
correlation <- cor(iris$Sepal.Length, iris$Petal.Length)
correlation
# Print the Pearson correlation coefficient
print(correlation)
# Create a correlation matrix
cor_matrix <- cor(iris[, 1:4])
cor_matrix

You might also like