If you try to use the $ operator to access an element of an atomic vector, you will raise the error $ operator is invalid for atomic vectors.

You can solve this error by using single [] or double square brackets [[]], use the getElement() function, or convert the vector to a Data Frame, then use the $ operator.

This tutorial will go through how to solve this error with code examples.


Example

Let’s look at an example of a vector:

# Define vector

vec <- c(2, 4, 6, 8, 10)

# Assign names to elements

names(vec) <- c('i', 'j', 'k', 'l', 'm')

print(vec)
 i  j  k  l  m 
 2  4  6  8 10

Let’s try to access the value under ‘j’:

print(vec$j)
Error in vec$j : $ operator is invalid for atomic vectors

We raise the error because the $ operator is not suitable for accessing elements in atomic vectors. We can check the documentation for the $ operator using ?”$”

?"$"

Under “Details”, we can read:

$ is only valid for recursive objects

We can verify that the vector is atomic by using is.atomic:

print(is.atomic(vec))
[1] TRUE

Solution #1: Use Single or Double Square Brackets

The usual form of indexing an atomic vector is single brackets, and double square brackets select a single element dropping names. We can solve this error by accessing the elements by name using the single or double square brackets notation. Let’s look at the revised code:

# Define vector

vec <- c(2, 4, 6, 8, 10)

# Assign names to elements

names(vec) <- c('i', 'j', 'k', 'l', 'm')

print(vec['j'])

print(vec[['j']])

Let’s run the code to get the result:

j 
4
[1] 4

If we index a vector using single square brackets, we retain the value name, whereas we drop the name if we use double brackets.

Solution #2: Use the getElement() Function

We can solve this error by using the getElement() function, where we specify the vector and the element name as arguments for the function. Let’s look at the revised code:

# Define vector

vec <- c(2, 4, 6, 8, 10)

# Assign names to elements

names(vec) <- c('i', 'j', 'k', 'l', 'm')

print(getElement(vec,'j'))
[1] 4

Solution #3: Convert Vector to Data Frame and use the Dollar Sign ($) Operator

We can use the $ operator by first converting the vector to a Data Frame using as.data.frame(). Let’s look at the revised code:

# Define vector

vec <- c(2, 4, 6, 8, 10)

# Assign names to elements

names(vec) <- c('i', 'j', 'k', 'l', 'm')

data <- as.data.frame(t(vec))

print(data)

print(is.atomic(data))
print(is.recursive(data))

We can also verify that the data object is not an atomic object and is, in fact, recursive using is.atomic() and is.recursive().

  i j k l  m
1 2 4 6 8 10

[1] FALSE
[1] TRUE

Next, we will access the value for ‘j‘ in the data frame:

print(data$j)
[1] 4

Difference Between an Atomic and Recursive Object

An atomic data object can only contain elements from one and only one of the six basic atomic vector types: logical, integer, real, complex, string (or character, and raw. Examples of atomic objects are vectors, matrices, and arrays. A recursive object can contain data objects of any mode, and examples of recursive objects include lists, data frames, and functions.

What is an Atomic Vector in R?

An atomic vector is any one-dimensional, homogeneous (all contents must be of the same type) data object. We can create an atomic vector using the c() or vector() functions.

What is the Dollar-Sign ($) Operator in R?

We can use the dollar sign “$” operator to select a variable/column, assign new values to a variable/column, or add a new variable or column in an R object. The $ operator is only valid for recursive objects (and NULL). Let’s look at an example of using $ on a list.

list_x <- list("electron"=0.51, "proton"=938.3, "muon"="105.7", "neutron"=939.6)
print(list_x$electron)
[1] 0.51

Summary

Congratulations on reading to the end of this tutorial. The $ operator is only suitable for recursive objects. If you want to index an atomic vector, you need to use single or double brackets or getElement. Alternatively, you can convert the vector to a data frame and use the $ operator.

For further reading on R related errors, go to the article: How to Solve R Error: Names do not match previous names

Go to the online courses page on R to learn more about coding in R for data science and machine learning.

Have fun and happy researching!