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
2
Q
For the dataset “murders” with variables “total” and “population”, do the following:
- compute the “murder_rate” per 100,000
- order the “states” in decreasing order by “murder_rate”
A
- murder_rate <- murders$total/murders$population * 100000
- murders$state[order(murder_rate,decreasing=TRUE)]
3
Q
Do the following:
- Create a vector “city” with the following city names:
- “Beijing”, “Lagos”, “Paris”, “Rio de Janeiro”, “San Juan”, “Toronto”
- Create a vector “temp” with the following temperatures in Fahrenheit
- 35, 88, 42, 84, 81, 30
- Use vector arithmetics to convert “temp” to Celsius
- C=5/9×(F−32)
- Create a data frame called “city_temps” with the city names and temperatures in Celsius.
A
- # Assign city names to
city
- city <- c(“Beijing”, “Lagos”, “Paris”, “Rio de Janeiro”, “San Juan”, “Toronto”)
- # Store temperature values in
temp
- temp <- c(35, 88, 42, 84, 81, 30)
- # Convert temperature into Celsius and overwrite the original values of ‘temp’ with these Celsius values
- temp <- (5/9) * (temp-32)
- # Create a data frame
city_temps
- city_temps <- data.frame(name=city, temperature=temp)
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
5
Q
Display the index of True values in the following vector:
- x <- c(FALSE, TRUE, FALSE, TRUE, TRUE, FALSE)
A
- which(x)
6
Q
- Using the “match” function, find the index of the following states using the “state” variable within the “murders” dataset:
- New York
- Florida
- Texas
- List the murder rate of the three states
A
- index <- match(c(“New York”, “Florida”, “Texas”),murders$state)
- index
- murder_rate[index]
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
8
Q
- Compute the per 100,000 murder rate for each state and store it in an object called murder_rate.
- 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.
- Get the indices of the entries that are below 1
- Get the names of the states with murder rates lower than 1
A
- # Store the murder rate per 100,000 for each state, in murder_rate
- murder_rate <- murders$total/murders$population*100000
- # Store the murder_rate < 1 in low
- low <- murder_rate < 1
- # Get the indices of entries that are below 1
- which(low)
- # Names of states with murder rates lower than 1
- murders$state[low]
9
Q
- Compute the per 100,000 murder rate for each state and store it in an object called murder_rate.
- 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.
- Use the & operator to create a new object ind that is true when low is true and the state is in the Northeast
- Use the brackets [ and ind to show the state names that satisfy this condition
A
- # Store the murder rate per 100,000 for each state, in
murder_rate
- murder_rate <- murders$total/murders$population*100000
- # Store the
murder_rate \< 1
inlow
- low <- murder_rate < 1
- # Create a vector ind for states in the Northeast and with murder rates lower than 1.
- ind <- (murders$region == “Northeast”) & (low)
- # Names of states in
ind
- murders$state[ind]
10
Q
- Compute average murder rate and store in avg using
mean
- How many states have murder rates below avg ? Check using sum
A
- # Compute average murder rate and store in avg using
mean
- avg <- mean(murder_rate)
- # How many states have murder rates below avg ? Check using sum
- sum(murder_rate < avg)
11
Q
- Store the 3 abbreviations (AK, MI, and IA) in the variable “abbs” in a vector
- Match the abbs to the murders$abb and store in ind
- Print state names from ind
A
- # Store the 3 abbreviations in abbs in a vector (remember that they are character vectors and need quotes)
- abbs <- c(“AK”, “MI”, “IA”)
- # Match the abbs to the murders$abb and store in ind
- ind <- match(abbs, murders$abb)
- # Print state names from ind
- murders$state[ind]
12
Q
- Define a character vector with the abbreviations MA, ME, MI, MO, MU.
- Use the % operator to create a logical vectors that is TRUE when the abbreviation is in murders$abb.
A
- # Store the 5 abbreviations in
abbs
. (remember that they are character vectors)- abbs <- c(“MA”,”ME”,”MI”,”MO”,”MU”)
- # Use the %in% command to check if the entries of abbs are abbreviations in the the murders data frame
- abbs %in% murders$abb
13
Q
We are again working with the characters abbs <- c(“MA”, “ME”, “MI”, “MO”, “MU”):
- 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.
- Show the entries of abbs that are not actual abbreviations.
A
- # 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)
- # What are the entries of abbs that are not actual abbreviations
- abbs[ind]