R STUDIO The R file will consist of all the code and should be executable (you s
ID: 3751405 • Letter: R
Question
R STUDIO
The R file will consist of all the code and should be executable (you should use R script).
Copy your R script into a word document, submit both R, and word (doc/docx) files.
All the answers, other than the actual codes, are to be noted as comments
Name the file “LastName_Assignment1” with appropriate file extension (.doc and .R)
Your script should follow the format below:
#1 Basic operations, assignments
#a
x <- 4
y <- 5
z <- x+y
z
#output: 9
#b
x <- 1
y <- 5
z <- x+y
z
Question:Matrices
a. Provide the code to create matrix X
X=
b. Provide the code to assign the value 2 for all the elements in the matrix that are less than 5
c. Assign the values in 2nd and 3rd column to the new matrix Y
d. Assign row names of X as R1, R2, R3
e. Assign column names of X as C1 to C3
f. Add a new row named R4 with the following values of 3, 6, 9
g. Add a new column named C4 with the following values of 1,3,5,7
h. Assign 0s for all elements of the second column C2
i. Print the final X matrix
2 5 8 3 6 9 4 7 10Explanation / Answer
x <- matrix(2:10, nrow = 3, ncol = 3)
x
// assign the value 2 for all the elements in the matrix that are less than 5
x[x<5] <- 2
x
//Assign row names of X as R1, R2, R3
rownames(x)[3] <- "R3"
rownames(x)[2] <- "R2"
rownames(x)[1] <- "R1"
x
//Assign column names of X as C1 to C3
colnames(x)[3] <- "C3"
colnames(x)[2] <- "C2"
colnames(x)[1] <- "C1"
x
//Add a new row named R4 with the following values of 3, 6, 9
x <- rbind(x,c(3,6,9))
rownames(x)[4] <- "R4"
x
//Add a new column named C4 with the following values of 1,3,5,7
x <- cbind(x,c(1, 3, 5, 7))
colnames(x)[4] <- "C4"
x
// Assign 0s for all elements of the second column C2
x[,2] <- 0
x
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.