Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to access the first element of the vector.
R Programming
vec <- c(10, 20, 30) first_element <- vec[[1]]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as an index, which returns an empty vector.
Using negative indices incorrectly to exclude elements.
✗ Incorrect
In R, vector indexing starts at 1, so the first element is accessed with index 1.
2fill in blank
mediumComplete the code to access the third element of the vector.
R Programming
numbers <- c(5, 15, 25, 35) third <- numbers[[1]]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or 4 to access the third element.
Confusing zero-based indexing from other languages.
✗ Incorrect
The third element is accessed with index 3 because R uses 1-based indexing.
3fill in blank
hardFix the error in the code to correctly access the second element.
R Programming
values <- c(100, 200, 300) second_value <- values[[1]]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or negative numbers as indices.
Using index 3 which accesses the third element instead.
✗ Incorrect
Index 2 accesses the second element in R vectors.
4fill in blank
hardFill both blanks to create a vector of squares for elements greater than 2.
R Programming
vec <- c(1, 2, 3, 4, 5) squares <- vec[vec [1] 2]^[2] 2
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > for filtering.
Using ** which is not the exponent operator in R.
✗ Incorrect
We select elements greater than 2 using > and square them using the ^ operator.
5fill in blank
hardComplete the code to access the last element of the vector.
R Programming
vec <- c(10, 20, 30, 40, 50) last_element <- vec[[1]]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using -1, which excludes the first element in R.
Hardcoding the value 5.
Using 0 as an index.
✗ Incorrect
In R, the last element can be accessed using length(vec) as the index since indexing is 1-based.