stats 20 midterm 2

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/49

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

50 Terms

1
New cards

Use a while loop to compute the number of terms required before the product

1 · 2 · 3 · 4 · 5 · . . .

reaches above 1000.

Hint: The answer is 6. Your code should only output the number 6 at the end. You do not actually have

to do any computations, just write the code that would do the computations for you and output the correct number. You do not have to write a function.

# SOLUTION

n_terms <- 0

cum_prod <- 1

i <- 1

while (cum_prod * (i + 1) < 1000) {

n_terms <- n_terms + 1

cum_prod <- cum_prod * i

i <- i + 1

}

n_terms

[1] 6

2
New cards

We want to figure out how many numbers we can multiply together (starting from 1) before the product becomes bigger than 1000.

1 × 2 × 3 × ... × ? < 1000

We’ll keep track of that using a while loop in R.

3
New cards

You're starting with 0 terms multiplied so far.
This variable will count how many numbers we use.

n_terms <- 0

4
New cards

This stands for cumulative product (just a fancy word for multiplying things together).
We start at 1 because multiplying by 1 doesn't change anything.

cum_prod <- 1

5
New cards

We start counting from 1.
This variable will hold the number we're about to multiply by.

i <- 1

6
New cards

This is the loop condition.

It says:
“As long as the result of multiplying the current product by the next number (i + 1) is still less than 1000… keep going.”

This helps stop before we go over 1000.

while (cum_prod * (i + 1) < 1000)

7
New cards

Inside the while loop:

We're using one more number, so we add 1 to our count.

We multiply our current product by i.
This is like doing:

1 × 2 × 3 × ... × i

n_terms <- n_terms + 1

cum_prod <- cum_prod * i

i <- i + 1

8
New cards

What does %% do in R?

It returns the remainder after division (modulo operator).
Example: 5 %% 2 returns 1.

9
New cards

What is the basic structure of the isleapyear() function?

isleapyear <- function(year) {
out <- FALSE
# logic here…
out
}

10
New cards

What does year %% 4 == 0 check?

It checks if the year is divisible by 4.

11
New cards

When do we set out <- TRUE in the function?

When the year is divisible by 4 (first check), and not disqualified by the 100-rule unless it passes the 400-rule.

12
New cards

What does the second if (year %% 100 == 0) check for?

It checks if the year is divisible by 100. If true, it disqualifies it as a leap year unless…

13
New cards

What does year %% 400 == 0 mean?

It checks if the year is divisible by 400. If true, it re-qualifies the year as a leap year.

14
New cards

What’s a cleaner, vectorized version of isleapyear()?

isleapyear <- function(year) {
(year %% 4 == 0) & ((year %% 100 != 0) | (year %% 400 == 0))
}

15
New cards

What does the expression (year %% 4 == 0) & ((year %% 100 != 0) | (year %% 400 == 0)) mean?

Is divisible by 4 AND
(Not divisible by 100 OR divisible by 400)

16
New cards

What are some example outputs?

isleapyear(3) # FALSE
isleapyear(8) # TRUE
isleapyear(200) # FALSE
isleapyear(800) # TRUE

17
New cards

What is the goal of the areleapyears() function?

To take a numeric vector of years and return a logical vector of the same length indicating whether each year is a leap year.

18
New cards

What’s the simplest way to write a vectorized version using logic?

areleapyears

19
New cards

What does years %% 4 == 0 do in vectorized form?

It returns a logical vector indicating which years are divisible by 4.

20
New cards

What is vapply() used for in this context?

It applies a function (isleapyear) to each element of a vector and ensures the output type is consistent.

areleapyears

21
New cards

How can a for loop be used to achieve the same goal?

areleapyears

22
New cards

How can you write areleapyears() without calling isleapyear()?

areleapyears

23
New cards

What output should you get for this input?

areleapyears(years = c(3, 8, 200, 800))

[1] FALSE TRUE FALSE TRUE

24
New cards

What type of data structure is big_list?

A named list with nested lists inside it.

25
New cards

What does big_list[[3]] return?

The third element of the top-level list, which is the \"Star Wars\" list.

26
New cards

What does big_list[[3]][[1]] return?

The \"characters\" list inside the \"Star Wars\" list.

27
New cards

What does big_list[[3]][[1]][[3]] return?

The third character in the \"characters\" list — which is R2D2.

28
New cards

What does big_list[[3]][[1]][[3]][[1]] return?

The \"species\" of R2D2, which is \"robot\".

29
New cards

What is the formula to convert feet and inches to total inches?

total_inches = feet * 12 + inches

30
New cards

What is the conversion factor from inches to centimeters?

1 inch = 2.54 cm

31
New cards

What is the R function to convert feet and inches to centimeters?

imperialtometric

32
New cards

What does imperialtometric(1, 2) return?

35.56 cm

33
New cards

What does imperialtometric(inches = 2) return?

5.08 cm

34
New cards

What is the formula to convert centimeters to total inches?

total_inches = cm / 2.54

35
New cards

How do you convert total inches to feet and inches in R?

feet

36
New cards

What is the R function to convert centimeters to feet and inches?

metrictoimperial

37
New cards

What does metrictoimperial(35.56) return?

feet = 1, inches = 2

38
New cards

Why use %/% and %% instead of /?

%/% gives the whole number (feet); %% gives the remainder (inches).

39
New cards

What is the output of this code?
x <- 1:3
y <- c(TRUE, FALSE)
x[y]

[1] 1 3

✅ Logical indexing repeats y to match length of x, so it's equivalent to x[c(TRUE, FALSE, TRUE)].

40
New cards

What is the output of this code?
y <- c(NA, 1)
is.na(y[2])

[1] FALSE

✅ y[2] is 1, not NA, so is.na() returns FALSE.

41
New cards

What is the output of this code?
z <- list(x = 1, y = 2)
z$x

[1] 1

✅ $x accesses the element named "x" in the list.

42
New cards

What is the output of this code?
z <- list(x = 1, y = 2)
z[1]

$x
[1] 1

✅ z[1] returns a sublist containing the first element — not just the value.

43
New cards

What is the output of this code?
z <- list(x = 1, y = 2)
z[[1]]

[1] 1

✅ [[1]] directly extracts the value from the first element in the list.

44
New cards

What is the output of this code?
z <- list(x = 1, y = 2)
names(z)

[1] "x" "y"

✅ names() returns the names of the list elements.

45
New cards

What is the output of this code?
x <- c("A", "B", "C")
x[1:2]

[1] "A" "B"

✅ Standard indexing with a range.

46
New cards

What is the output of this code?
x <- matrix(1:6, nrow = 2)
x[ , 2]

[1] 3 4

✅ Extracts the 2nd column of the matrix as a vector.

47
New cards

What is the output of this code?
x <- matrix(1:6, nrow = 2)
x[1, ]

[1] 1 3 5

✅ Extracts the 1st row of the matrix.

48
New cards

What is the output of this code?
x <- matrix(1:6, nrow = 2)
x[2, 3]

[1] 6

✅ This accesses the value in the 2nd row, 3rd column.

49
New cards

What is the output of this code?
x <- c(TRUE, FALSE, TRUE)
which(x)

[1] 1 3

✅ which() returns the positions of TRUE values.

50
New cards

What is the output of this code?
x <- c(TRUE, FALSE, TRUE)
sum(x)

[1] 2

✅ In R, TRUE = 1 and FALSE = 0, so sum() adds up the number