[go: up one dir, main page]

0% found this document useful (0 votes)
92 views5 pages

R Questions

The document provides examples of R code and questions about R functions and operations: 1. It asks which code would change values in a matrix based on a vector. 2. It asks which code computes a mathematical expression. 3. It asks which code produces a symmetric matrix using nested for loops. The document tests knowledge of R functions, loops, matrices, and mathematical operations.

Uploaded by

Natiq
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)
92 views5 pages

R Questions

The document provides examples of R code and questions about R functions and operations: 1. It asks which code would change values in a matrix based on a vector. 2. It asks which code computes a mathematical expression. 3. It asks which code produces a symmetric matrix using nested for loops. The document tests knowledge of R functions, loops, matrices, and mathematical operations.

Uploaded by

Natiq
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/ 5

R Exercises

1- Given the below matrix, what changes would the following command generate
iMatrix[1,]<-c(1, 2, 3)
4 5 2 1
(7 1 6 4)
8 3 2 6

a. The first three values of the first column would change into the values within the
vector.
b. The first three values of the first row would change into the values within the
vector.
c. The first three values of the first row would be added to the values within the
vector.
d. None of the above

Answer: d

2- Choose the R command to compute the following:


4
√|𝑥 − 2|⁄𝑥 2

a. (sqrt (abs (x-2))*4)/ (x*2)


b. (sqrt (abs (x-2))^(1/4)/ (x^2)
c. ((abs (x-2))*4)/ (x*2)
d. ((abs (q-2))^(1/4))/ (q^2)

Answer: d

3- Use a nested “for loop” that produces a symmetric matrix as following:


[,1] [,2] [,3] [,4] [,5]
[1,] 0 1 2 3 4
[2,] 1 0 1 2 3
[3,] 2 1 0 1 2
[4,] 3 2 1 0 1
[5,] 4 3 2 1 0

a. mtrx <- matrix(nrow = 5, ncol = 5)


for (i in 1:5) {
for (j in 1:5) {
mtrx[i, j] <- abs(i - j)
}
}

b. mtrx <- matrix(nrow = 5, ncol = 5)


for (i in 1:5) {
for (j in 1:5) {
mtrx[i, j] <- i - j
}
}
c. mtrx <- matrix(nrow = i, ncol = j)
for (i in 1:5) {
for (j in 1:5) {
mtrx[i, j] <- i - j
}
}

d. mtrx <- matrix(nrow = 5, ncol = 5)


for (i in 1:5) {
for (j in 1:5) {
mtrx[i, j] <- i - 1
}
}

Answer: a

4- Which of the following code does not identify if a given number is positive or negative
or zero?

a. x <- 0
if (x < 0) {
print("Negative number")
} else if (x > 0) {
print("Positive number")
} else
print("Zero")

b. if (x < 0) {
print("Negative number")
}
else if (x > 0) {
print("Positive number")
}
else (x == 0) {
print("Zero")
}

c. if (x < 0) {
print("Negative number")
else if (x > 0) {
print("Positive number")
else
print("Zero")
}
}
d. if (x < 0) {
print("Negative number")
if (x > 0) {
print("Positive number")
if (x==0){
print("Zero")
}
}
}

Answer: a

5- X TRUE, Y FALSE, Z NA

Which of the followings gives all NA as a result?

a. X & X, X | Y, Y & Z
b. X & Z, X & Y, Y | Z
c. X & Z, Z | Y, Y | Z
d. Z & X, Z | Z, X & X

Answer: c

6- Which of the following function defines mean of 3 numbers?

a. mean_of_three <- function(num1, num2) {


sum <- num1 + num2/3
return(sum)
}

b. mean_of_three <- function("num1", "num2", "num3") {


mum <- "num1" + "num2" + "num3"/3
return(sum)
}

c. mean_of_three <- function(num1, num2, num3) {


sum <- num1 + num2 + num3/3
return(sum)
}

d. mean_of_three <- function(num1, num2, num3) {


sum <- (num1 + num2 + num3)/3
return(sum)
}

Answer: d
7- Which of the following functions produce the same results as the function below?

f <- function(x) {
for(i in 1:x) {
y <- i^2
print(y)
}
}
f(3)

a. f <- function() {
for(i in 1:2) {
print(i^2)
}
}
f()

b. f <- function() {
for(i in 1:5) {
print(i^2)
}
}

f()

c. f <- function() {
for(i in 1:3) {
print(i^2)
}
}

f()

d. f <- function(a) {
for(i in 1:5) {
print(i^2)
}
}

f()

Answer: c
8- Write a function to specify the exponent using the second argument directly, Sets the
default of the exponent to 3.

a. fun1 <- function(n, m) { n^m }

b. fun1 <- function(n, m = 2) { n^m }


c. fun1 <- function(n, m = 3) { n^m }

d. fun1 <- function(n, m) { n^3 }

Answer: c

9- Which of the folowing function define min-max normalization


(minmax_norm=(x-xmin)/(xmax-xmin))?

a. minmax_norm <- function(x){


m <- x-min(x)
n <- max(x)-min(x)
b <- n/m
return(b)
}

b. minmax_norm <- function(x){


n <- x-min(x)
m <- max(x)-min(x)
minmax_norm <- n/m
return(minmax_norm)
}

c. minmax_norm <- function(m, n){


n <- x-min(x)
m <- max(x)-min(x)
minmax_norm <- n/m
return(minmax_norm)
}

d. minmax_norm <- function(m, n, b){


m <- x-min(x)
n <- max(x)-min(x)
b <- n/m
return(b)
}

Answer:b

You might also like