Part a r Programming
Part a r Programming
Part A
1) Write a program to create a 3 X 3 matrices A and B and perform the following operations
a. AT.B
b. BT.(A.AT)
c. (A.AT).BT
d. [(B.BT)+(A.AT)-100I3]-1
# Create matrices A and B
A <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3)
B <- matrix(c(9, 8, 7, 6, 5, 4, 3, 2, 1), nrow = 3)
# Calculate AT.B
result_a <- t(A) %*% B
# Calculate BT.(A.AT)
result_b <- t(B) %*% (A %*% t(A))
# Calculate (A.AT).BT
result_c <- (A %*% t(A)) %*% B
# Print results
print("Result of AT.B:")
print(result_a)
print("Result of BT.(A.AT):")
print(result_b)
print("Result of (A.AT).BT:")
print(result_c)
print(first_element)
print(third_element)
vi=c(0,5,10,15,20)
vi2=c(5,10,15,20,25)
y_data=(vi+vi2)/2
freq=c(5,25,40,17,13)
6) 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.
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)
)
subset_a<-data[data$ItemPrice>=350,]
subset_c<-data[data$ItemPrice>=300&data$ItemPrice<=700,]
total_price<-sum(data$ItemPrice)
item_details<-data.frame(
itemCode=c(1001,1002,1003,1004,1005),
ItemQtyonHand=c(10,15,20,5,12),
ItemReorderLvl=c(2,5,3,4,6)
)
merge_data<-merge(data,item_details,by="itemCode")
#is.factor
isfactors<-is.factor(marital_status)
print(isfactors)
#Add new level widowed to the factor and add the same level to the factor marital_status
factor(append(as.character(marital_status),"Widowed"))