R Pgms 30
R Pgms 30
Write an R program to create a Data Frame with following details and do the
following operations.
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)
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)
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