r_vector_indexing Flashcards

1
Q

For the following vector “heights”, multiple each value by 2.54:

  • heights <- c(69, 62, 66, 70, 70, 73, 67, 73, 67, 70)
A
  • heights * 2.54
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

For the dataset “murders” with variables “total” and “population”, do the following:

  1. compute the “murder_rate” per 100,000
  2. order the “states” in decreasing order by “murder_rate”
A
  1. murder_rate <- murders$total/murders$population * 100000
  2. murders$state[order(murder_rate,decreasing=TRUE)]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Do the following:

  1. Create a vector “city” with the following city names:
    • “Beijing”, “Lagos”, “Paris”, “Rio de Janeiro”, “San Juan”, “Toronto”
  2. Create a vector “temp” with the following temperatures in Fahrenheit
    • 35, 88, 42, 84, 81, 30
  3. Use vector arithmetics to convert “temp” to Celsius
    • C=5/9×(F−32)
  4. Create a data frame called “city_temps” with the city names and temperatures in Celsius.
A
  1. # Assign city names to city
    • city <- c(“Beijing”, “Lagos”, “Paris”, “Rio de Janeiro”, “San Juan”, “Toronto”)
  2. # Store temperature values in temp
    • temp <- c(35, 88, 42, 84, 81, 30)
  3. # Convert temperature into Celsius and overwrite the original values of ‘temp’ with these Celsius values
    • temp <- (5/9) * (temp-32)
  4. # Create a data frame city_temps
    • city_temps <- data.frame(name=city, temperature=temp)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Display the values from the ‘murder’ dataset where:

  • region == “West”
  • murder rate <= 1
A
  • west <- murder$region == “West”
  • safe <- murder_rate <= 1
  • index <- safe & west
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Display the index of True values in the following vector:

  • x <- c(FALSE, TRUE, FALSE, TRUE, TRUE, FALSE)
A
  • which(x)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. Using the “match” function, find the index of the following states using the “state” variable within the “murders” dataset:
    • New York
    • Florida
    • Texas
  2. List the murder rate of the three states
A
  1. index <- match(c(“New York”, “Florida”, “Texas”),murders$state)
    • index
  2. murder_rate[index]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Find out if the following states are represented within the “state” variable of the “murders” dataset:

  • Boston
  • Dakota
  • Washington
A
  • c(“Boston”, “Dakota”, “Washington”) %in% murders$state
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. Compute the per 100,000 murder rate for each state and store it in an object called murder_rate.
  2. Then use the logical operators to create a logical vector, name it low, that tells us which entries of murder_rate are lower than 1.
  3. Get the indices of the entries that are below 1
  4. Get the names of the states with murder rates lower than 1
A
  1. # Store the murder rate per 100,000 for each state, in murder_rate
    • murder_rate <- murders$total/murders$population*100000
  2. # Store the murder_rate < 1 in low
    • low <- murder_rate < 1
  3. # Get the indices of entries that are below 1
    • which(low)
  4. # Names of states with murder rates lower than 1
    • murders$state[low]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. Compute the per 100,000 murder rate for each state and store it in an object called murder_rate.
  2. Then use the logical operators to create a logical vector, name it low, that tells us which entries of murder_rate are lower than 1.
  3. Use the & operator to create a new object ind that is true when low is true and the state is in the Northeast
  4. Use the brackets [ and ind to show the state names that satisfy this condition
A
  1. # Store the murder rate per 100,000 for each state, in murder_rate
    • murder_rate <- murders$total/murders$population*100000
  2. # Store the murder_rate \< 1 in low
    • low <- murder_rate < 1
  3. # Create a vector ind for states in the Northeast and with murder rates lower than 1.
    • ind <- (murders$region == “Northeast”) & (low)
  4. # Names of states in ind
    • murders$state[ind]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. Compute average murder rate and store in avg using mean
  2. How many states have murder rates below avg ? Check using sum
A
  1. # Compute average murder rate and store in avg using mean
    • avg <- mean(murder_rate)
  2. # How many states have murder rates below avg ? Check using sum
    • sum(murder_rate < avg)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. Store the 3 abbreviations (AK, MI, and IA) in the variable “abbs” in a vector
  2. Match the abbs to the murders$abb and store in ind
  3. Print state names from ind
A
  1. # Store the 3 abbreviations in abbs in a vector (remember that they are character vectors and need quotes)
    • abbs <- c(“AK”, “MI”, “IA”)
  2. # Match the abbs to the murders$abb and store in ind
    • ind <- match(abbs, murders$abb)
  3. # Print state names from ind
    • murders$state[ind]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. Define a character vector with the abbreviations MA, ME, MI, MO, MU.
  2. Use the % operator to create a logical vectors that is TRUE when the abbreviation is in murders$abb.
A
  1. # Store the 5 abbreviations in abbs. (remember that they are character vectors)
    • abbs <- c(“MA”,”ME”,”MI”,”MO”,”MU”)
  2. # Use the %in% command to check if the entries of abbs are abbreviations in the the murders data frame
    • abbs %in% murders$abb
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

We are again working with the characters abbs <- c(“MA”, “ME”, “MI”, “MO”, “MU”):

  1. In a previous exercise we computed the index abbs%in%murders$abb.
    • Use which and the ! operator to get the index of the entries of abbs that are not abbreviations.
  2. Show the entries of abbs that are not actual abbreviations.
A
  1. # Use the which command and ! operator to find out which abbreviation are not actually part of the dataset and store in ind
    • ind <- which(!abbs%in%murders$abb)
  2. # What are the entries of abbs that are not actual abbreviations
    • abbs[ind]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly