Vectors are the basic way to store many values in R. They keep data in order and make it easy to work with numbers, words, or logical values.
Why vectors are the fundamental data structure in R Programming
c(value1, value2, value3, ...)
The c() function combines values into a vector.
All values in a vector must be the same type (all numbers, or all text, etc.).
numbers <- c(10, 20, 30, 40)
words <- c("apple", "banana", "cherry")
flags <- c(TRUE, FALSE, TRUE)
This program creates two vectors: one for ages and one for names. It prints both vectors and then calculates and prints the average age.
ages <- c(25, 30, 22, 40) names <- c("Anna", "Bob", "Cara", "Dan") # Print ages vector print(ages) # Print names vector print(names) # Calculate average age average_age <- mean(ages) print(average_age)
Vectors are simple but powerful for storing data in R.
Mixing types in a vector will convert all values to the most flexible type (usually text).
Many R functions expect vectors as input, so understanding them helps with most tasks.
Vectors hold many values of the same type in order.
They are easy to create and use with the c() function.
Vectors are the building blocks for more complex data in R.
Practice
Solution
Step 1: Understand vector properties in R
Vectors store multiple values but all must be of the same type, arranged in order.Step 2: Compare with other data structures
Unlike lists, vectors cannot hold mixed types, and they are the basic building block for data in R.Final Answer:
Because they hold many values of the same type in order -> Option CQuick Check:
Vectors = same type, ordered values [OK]
- Thinking vectors can hold mixed data types
- Confusing vectors with lists
- Assuming vectors store single values only
Solution
Step 1: Recall vector creation syntax in R
The functionc()combines values into a vector.Step 2: Check other options
vector()creates an empty vector with specified length and mode,list()creates a list, andarray()creates multi-dimensional arrays.Final Answer:
c(1, 2, 3) -> Option DQuick Check:
Use c() to create vectors [OK]
- Using vector() with values directly
- Confusing list() with vector creation
- Using array() for simple vectors
v <- c(10, 20, 30) typeof(v)
Solution
Step 1: Understand default numeric type in R vectors
By default, numbers with decimals or without are stored as double precision floating point numbers.Step 2: Check the vector values
The vector contains 10, 20, 30 which are numeric literals without explicit integer suffix, so they are doubles.Final Answer:
"double" -> Option AQuick Check:
Numeric vectors default to double type [OK]
- Assuming numeric literals are integers by default
- Confusing list type with vector type
- Expecting character type from numbers
v <- c(1, "two", 3)
Solution
Step 1: Analyze vector type coercion rules
R forces all elements in a vector to be the same type, so it converts numbers to characters if any element is character.Step 2: Check the given vector elements
Since "two" is a character, 1 and 3 are converted to characters, so the vector becomes character type.Final Answer:
The vector contains mixed types, so R converts all to character -> Option AQuick Check:
Mixed types in vector cause coercion to character [OK]
- Expecting syntax error from mixed types
- Assuming numeric values stay numeric
- Thinking vector will be empty
Solution
Step 1: Create a numeric vector of ages
Usec()to create a vector holding the ages.Step 2: Add 5 to each element
Adding a number to a vector adds it to each element element-wise.Final Answer:
ages <- c(25, 30, 22, 28) ages <- ages + 5 -> Option BQuick Check:
Vector arithmetic applies element-wise [OK]
- Using list() instead of c() for vector
- Multiplying instead of adding
- Subtracting instead of adding
Ready to prove it?
Pass the test and this pattern moves unknown
