stats 20 midterm 2

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

1/252

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.

253 Terms

1
New cards

What does %% do in R?

It returns the remainder of division (modulo operator).

2
New cards

What does %/% do in R?

It returns the integer quotient of a division.

3
New cards

What function sets the working directory?

setwd()

4
New cards

What function shows your current working directory?

getwd()

5
New cards

How do you create a vector with the values 1, 2, and 3?

c(1, 2, 3)

6
New cards

c(1, 2, 3)

length()

7
New cards

What function gives you the type of storage for a vector?

mode()

8
New cards

How do you repeat a vector [1, 2] three times?

rep(c(1, 2), times = 3)

9
New cards

What does sample() do?

Randomly samples elements from a vector.

10
New cards

What does o[x] do if x is numeric?

Returns elements at positions specified in x.

11
New cards

What does o[-x] do?

Returns all elements except the ones at positions in x.

12
New cards

How do you coerce a vector to numeric?

as.numeric(x)

13
New cards

What does logical() create?

An empty logical vector.

14
New cards

What function creates a matrix in R?

matrix()

15
New cards

How do you access the second row of a matrix A?

A[2, ]

16
New cards

What does summary() return for a numeric vector?

Min, 1st Qu., Median, Mean, 3rd Qu., Max

17
New cards

What function converts characters to numbers?

as.numeric()

18
New cards

What function gets the sine of a value?

sin()

19
New cards

What does rev() do?

Reverses a vector.

20
New cards

What is NaN?

"Not a Number" – result of invalid math like 0/0.

21
New cards

How do you check if a value is NA?

is.na(x)

22
New cards

How do you test if a number is infinite?

is.infinite(x)

23
New cards

What does ?mean do?

Opens help for the mean() function.

24
New cards

What’s the difference between ? and ???

? searches by function name; ?? does a broader search (keywords, topics).

25
New cards

What does which() do?

Returns indices of TRUE values in a logical vector.

26
New cards

How do you check if all elements are TRUE?

all(x)

27
New cards

How do you check if two objects are nearly equal?

all.equal(x, y)

28
New cards

What function creates a data frame?

data.frame()

29
New cards

How do you extract a column from a data frame called df named "age"?

df$age

30
New cards

What function returns the number of rows?

nrow(df)

31
New cards

What does str(df) show?

The structure of the data frame, including types and samples of data.

32
New cards

What does ggplot() do?

Initializes a ggplot object.

33
New cards

What function sets the mapping of x/y to variables?

aes()

34
New cards

What function saves a plot to file?

ggsave()

35
New cards

What are the three main loop/control structures?

for(), if()/else, while()

36
New cards

What function exits the current iteration in a loop early?

next

37
New cards

What function exits the entire loop?

break

38
New cards

How do you define a function?

function(x) { … }

39
New cards

What do |> and %>% do?

Pipe the result of one expression into the next.

40
New cards

What does ifelse() do?

Vectorized if-else conditional; returns values based on a logical test.

41
New cards

What does unlist() do?

Flattens a list to an atomic vector.

42
New cards

How do you check if a value is in a vector?

x %in% y

43
New cards

Write an R function isleapyear() that takes a single year as input and returns TRUE if it's a leap year, and FALSE otherwise. A year is a leap year if it is divisible by 4, but not divisible by 100 unless also divisible by 400.

is_leap_year <- function(year) {
(year %% 4 == 0) & ((year %% 100 != 0) | (year %% 400 == 0))
}
44
New cards

What is the output of isleapyear(3)?

[1] FALSE

Explanation:
3 is not divisible by 4, so it is not a leap year.

45
New cards

What is the output of isleapyear(8)?

[1] TRUE

Explanation:
8 is divisible by 4, and not divisible by 100, so it is a leap year.

46
New cards

What is the output of isleapyear(200)?

[1] FALSE

Explanation:
200 is divisible by 100 but not by 400, so it is not a leap year.

47
New cards

What is the output of isleapyear(800)?

[1] TRUE

Explanation:
800 is divisible by 4, by 100, and by 400. It is a leap year.

48
New cards

Given the nested list big_list, how is the data structured?

The list big_list has four top-level elements named:

  1. "None" – an empty list

  2. "One" – a list containing a single number: 1

  3. "Star Wars" – a list with:

    • A sublist called "characters" (with nested lists for Obi Wan, Luke Skywalker, and R2D2)

    • A sublist called "movies" (a list of 3 movie titles)

  4. "Four" – a character vector: "Even", "more", "entries"

So big_list looks like this at the top level:

list(

"None" = list(),

"One" = list(1),

"Star Wars" = list( # <-- third element

characters = list( # <-- first element inside

"Obi Wan" = list(species = "human", movies = 1:6),

"Luke Skywalker" = list(species = "human", movies = 4:6),

"R2D2" = list(species = "robot", movies = 1:6)

),

movies = list(...) # <-- second element inside

),

"Four" = c("Even", "more", "entries")

)

49
New cards

How do you extract the character "robot" from big_list using only numeric indices?

big_list[[3]][[1]][[3]][[1]]

Correct Output:

[1] "robot"
50
New cards

What does each step in big_list[[3]][[1]][[3]][[1]] do?

  1. big_list[[3]] → accesses the "Star Wars" list (3rd element).
  2. [[1]] → gets the "characters" sublist (first item inside "Star Wars").
  3. [[3]] → gets the R2D2 sublist (3rd character).
  4. [[1]] → retrieves the value of the first item in R2D2, which is "robot" under "species".
51
New cards

Write an R function imperial_to_metric() that takes two non-negative scalar arguments feet and inches and returns the total height in centimeters. Assumes: 12 inches = 1 foot, 1 inch = 2.54 cm. Include default values for both inputs.

imperial_to_metric <- function(feet = 0, inches = 0) {
  total_inches <- feet * 12 + inches
  c(cm = total_inches * 2.54)
}

Explanation: Converts total height to inches first, then multiplies by 2.54 to get centimeters. Returns a named vector with name \"cm\"

52
New cards

What is the output of the following call?

imperial_to_metric(feet = 1)
cm 30.48

Explanation: 1 foot = 12 inches → 12 * 2.54 = 30.48 cm

53
New cards

What is the output of the following call?

imperial_to_metric(inches = 2)
cm 5.08

Explanation: 2 inches * 2.54 = 5.08 cm

54
New cards

What is the output of the following call?
```r
imperialtometric(feet = 1, inches = 2)

cm 35.56

Explanation: 1 foot = 12 inches → 12 + 2 = 14 inches, then 14 * 2.54 = 35.56 cm

55
New cards

Write an R function metrictoimperial() that:

Takes a single non-negative scalar input cm (centimeters)

Converts the value into feet and inches

Returns a named numeric vector of length 2: c(feet = …, inches = …)

Uses default input value cm = 0

metric_to_imperial <- function(cm = 0) {
  total_inches <- cm / 2.54
  feet <- total_inches %/% 12
  inches <- total_inches %% 12
  c(feet = feet, inches = inches)
}

Explanation:

cm / 2.54 converts centimeters to inches

total_inches %/% 12 computes full feet

total_inches %% 12 gives the remaining inches

56
New cards

What is the output of the following?
metrictoimperial(30.48)

feet inches
1 0

Explanation:

30.48 cm is exactly 12 inches (1 foot). This function avoids outputting “0 feet and 12 inches,” which would be incorrect formatting.

57
New cards

What is the output of the following?
metrictoimperial(5.08)

feet inches
0 2

Explanation:

5.08 cm is exactly 2 inches. Correctly returns no feet and 2 inches.

58
New cards

What is the output of the following?
metrictoimperial(35.56)

feet inches
1 2

Explanation:

35.56 cm = 14 inches → 1 foot + 2 inches. This output avoids “26 inches” and formats the result correctly.

59
New cards

What does %% do in R?

It returns the remainder of division (modulo operator).

60
New cards

What does %/% do in R?

It returns the integer quotient of a division.

61
New cards

What function sets the working directory?

setwd()

62
New cards

What function shows your current working directory?

getwd()

63
New cards

How do you create a vector with the values 1, 2, and 3?

c(1, 2, 3)

64
New cards

What function returns the number of elements in a vector?

length()

65
New cards

What function gives you the type of storage for a vector?

mode()

66
New cards

How do you repeat a vector [1, 2] three times?

rep(c(1, 2), times = 3)

67
New cards

What does sample() do?

Randomly samples elements from a vector.

68
New cards

What does o[x] do if x is numeric?

Returns elements at positions specified in x.

69
New cards

What does o[-x] do?

Returns all elements except the ones at positions in x.

70
New cards

How do you coerce a vector to numeric?

as.numeric(x)

71
New cards

What does logical() create?

An empty logical vector.

72
New cards

What function creates a matrix in R?

matrix()

73
New cards

How do you access the second row of a matrix A?

A[2, ]

74
New cards

What does IQR() compute?

The interquartile range.

75
New cards

How do you get the cumulative sum of a vector?

cumsum(x)

76
New cards

What does summary() return for a numeric vector?

Min, 1st Qu., Median, Mean, 3rd Qu., Max

77
New cards

What function converts characters to numbers?

as.numeric()

78
New cards

What function gets the sine of a value?

sin()

79
New cards

What does rev() do?

Reverses a vector.

80
New cards

What is NaN?

"Not a Number" – result of invalid math like 0/0.

81
New cards

How do you check if a value is NA?

is.na(x)

82
New cards

How do you test if a number is infinite?

is.infinite(x)

83
New cards

What does ?mean do?

Opens help for the mean() function.

84
New cards

What’s the difference between ? and ???

? searches by function name; ?? does a broader search (keywords, topics).

85
New cards

What does which() do?

Returns indices of TRUE values in a logical vector.

86
New cards

How do you check if all elements are TRUE?

all(x)

87
New cards

How do you check if two objects are nearly equal?

all.equal(x, y)

88
New cards

What function creates a data frame?

data.frame()

89
New cards

How do you extract a column from a data frame called df named "age"?

df$age

90
New cards

What function returns the number of rows?

nrow(df)

91
New cards

What does str(df) show?

The structure of the data frame, including types and samples of data.

92
New cards

What does ggplot() do?

Initializes a ggplot object.

93
New cards

What function sets the mapping of x/y to variables?

aes()

94
New cards

What function saves a plot to file?

ggsave()

95
New cards

What are the three main loop/control structures?

for(), if()/else, while()

96
New cards

What function exits the current iteration in a loop early?

next

97
New cards

What function exits the entire loop?

break

98
New cards

How do you define a function?

function(x) { … }

99
New cards

What do |> and %>% do?

Pipe the result of one expression into the next.

100
New cards

What does ifelse() do?

Vectorized if-else conditional; returns values based on a logical test.