0
0
R Programmingprogramming~10 mins

Accessing elements ([], [[]], $) 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 v.

R Programming
v <- c(10, 20, 30)
first_element <- v[1]1
Drag options to blanks, or click blank then click option'
A{
B[[
C$
D[
Attempts:
3 left
💡 Hint
Common Mistakes
Using double brackets [[ ]] which are for lists or data frames.
Using $ which is for named list or data frame elements.
2fill in blank
medium

Complete the code to extract the first element from the list lst.

R Programming
lst <- list(a = 5, b = 10)
first_element <- lst[1]1
Drag options to blanks, or click blank then click option'
A[[
B{
C$
D[
Attempts:
3 left
💡 Hint
Common Mistakes
Using single brackets which return a list, not the element.
Using $ without the element name.
3fill in blank
hard

Fix the error in the code to access the element named 'b' in the list lst.

R Programming
lst <- list(a = 5, b = 10)
value <- lst[1]b
Drag options to blanks, or click blank then click option'
A$
B[[b]]
C[b]
D[['b']]
Attempts:
3 left
💡 Hint
Common Mistakes
Using brackets with unquoted names which causes an error.
Using double brackets without quotes around the name.
4fill in blank
hard

Fill both blanks to create a named vector and access the element named 'second'.

R Programming
vec <- c(first = 1, second = 2, third = 3)
value <- vec[1]"second"[2]
Drag options to blanks, or click blank then click option'
A[
B[[
C]
Attempts:
3 left
💡 Hint
Common Mistakes
Using double brackets which are not valid for vectors.
Forgetting to close the brackets.
5fill in blank
hard

Complete the code to create a list with named elements and access the element named 'age'.

R Programming
person <- list(name = "Alice", age = 30, city = "NY")
age_value <- person[1]"age"
Drag options to blanks, or click blank then click option'
A[
B[[
C]
Attempts:
3 left
💡 Hint
Common Mistakes
Using single brackets which return a sublist, not the element.
Not quoting the name inside the brackets.