R Flashcards

(15 cards)

1
Q

is the built-in R constant that represents a missing value in
datasets.

A

NA (or NA)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

is R’s primary data structure for tabular data, consisting of a list of
equal-length vectors.

A

Data frame (or data.frame)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

is the operator used for matrix multiplication in R.

A

%*%

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

is the R function used to determine the internal storage type of an
object.

A

typeof()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

is the R6 class operation that removes and returns the top item
from a stack ADT.

A

pop (or pop())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

R is a compiled language that requires code to be translated into machine
code before execution.

A
  1. FALSE — R is an interpreted language (underline “compiled”)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In R, variable names can start with a number or an underscore.

A

FALSE — Variable names cannot start with a number or underscore (underline
“can”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The next statement in R skips the current iteration of a loop and proceeds to
the next iteration.

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

R uses lexical scoping, meaning variables are looked up based on how
functions are nested in the code.

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

The double square brackets [[]] are used for subsetting vectors and returning
multiple elements.

A

FALSE — Double square brackets [[]] extract single elements from lists
(underline “multiple”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Write an R function named calculate_bmi() that:
● Accepts two parameters: weight_kg (numeric) and height_cm (numeric)
● Calculates BMI using the formula: BMI = weight_kg / (height_m)^2, where
height_m = height_cm/100
● Returns the BMI value rounded to 2 decimal places
● Includes a default value of 70 for weight_kg and 175 for height_cm

A

calculate_bmi <- function(weight_kg = 70, height_cm = 175) {
height_m <- height_cm / 100
bmi <- weight_kg / (height_m^2)
return(round(bmi, 2))
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Analyze the following R code and determine what will be printed:

x <- c(TRUE, FALSE, TRUE, FALSE)
y <- c(TRUE, TRUE, FALSE, FALSE)
result <- x & y
print(result)

A

[1] TRUE FALSE FALSE FALSE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write a for loop that calculates the sum of all even numbers from 1 to 20 and prints the
final sum.

A

sum_even <- sum(seq(2, 20, 2))
print(paste(“Sum of even numbers from 1 to 20:”, sum_even))

Or

sum_even <- 0
for (i in 1:20) {
if (i %% 2 == 0) {
sum_even <- sum_even + i
}
}
Print (sum)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Given the following data frame:
students <- data.frame(
id = 1:5,
name = c(“Ana”, “Ben”, “Carla”, “David”, “Elena”),
score = c(92, 67, 78, 45, 88),
stringsAsFactors = FALSE
)
Write R code to:
● Add a new column named status
● Assign “Pass” if score is ≥ 75, otherwise assign “Fail”
● Display only the name, score, and status columns for students who passed

A

students$status <- ifelse(students$score >= 75, “Pass”, “Fail”)
students[students$status == “Pass”, c(“name”, “score”, “status”)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Write a recursive function named factorial() that calculates the factorial of a non-negative
integer n (n! = n × (n-1) × … × 1). Test your function with n = 6.

A

factorial <- function(n) {
if (n <= 1) {
return(1)
} else {
return(n * factorial(n - 1))
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly