Question: In the editor, write R code that takes a square character matrix and checks if any of the character strings on the diagonal (top left

In the editor, write R code that takes a square character matrix and checks if any of the character strings on the diagonal (top left to bottom right) begin with the letter g, lowercase or uppercase. If satisfied, these specific entries should be overwritten with the string "HERE". Otherwise, the entire matrix should be replaced with an identity matrix of the same dimensions. Then, try your code on the following matrices, checking the result each time:

i. mymat <- matrix(as.character(1:16),4,4)

ii. mymat <- matrix(c("DANDELION","Hyacinthus","Gerbera", "MARIGOLD","geranium","ligularia", "Pachysandra","SNAPDRAGON","GLADIOLUS"),3,3)

iii. mymat <- matrix(c("GREAT","exercises","right","here"),2,2, byrow=T)

Hint: This requires some thoughtyou will find the functions diag from Section 3.2.1 and substr from Section 4.2.4 useful.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!