0
0
R Programmingprogramming~10 mins

Vector indexing (1-based) in R Programming - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A0
B1
C-1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as an index, which returns an empty vector.
Using negative indices incorrectly to exclude elements.
2fill in blank
medium

Complete 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'
A2
B0
C4
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or 4 to access the third element.
Confusing zero-based indexing from other languages.
3fill in blank
hard

Fix 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'
A2
B3
C-2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or negative numbers as indices.
Using index 3 which accesses the third element instead.
4fill in blank
hard

Fill 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'
A>
B<
C**
D^
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > for filtering.
Using ** which is not the exponent operator in R.
5fill in blank
hard

Complete 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'
A-1
B5
Clength(vec)
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using -1, which excludes the first element in R.
Hardcoding the value 5.
Using 0 as an index.