R Error: $-Operator is Invalid for Atomic Vectors (Examples)


In this tutorial you’ll learn how to fix the error message “$ operator is invalid for atomic vectors” in R.

The content of the post looks as follows:

Let’s get started:


Example 1: Using $-Operator for Atomic Vector [Error]

In this Example, I’ll illustrate how you should not use the $-operator. For this, we have to create a named vector object first:

vec <- 1:5                          # Create named vector
names(vec) <- letters[1:5]
vec                                 # Print named vector
# a b c d e 
# 1 2 3 4 5

Our vector contains five named numeric values. Let’s assume that we want to extract the value with the name “a” from our vector object using the $-operator:

vec$a                               # Trying to use $-operator for named vector
# Error in vec$a : $ operator is invalid for atomic vectors

As you can see it doesn’t work. The RStudio console returns the error message “$ operator is invalid for atomic vectors”.

Next, I’ll explain how to avoid this error message in R…


Example 2: Using $-Operator for Recursive Object [Fixing the Error]

In Example 2, I’ll illustrate how to modify a data object so that you don’t get the error “$ operator is invalid for atomic vectors”.

For this, we have to convert our named vector to a data.frame object:

data <- data.frame(as.list(vec))    # Convert named vector to data.frame
data                                # Print data frame
#   a b c d e
# 1 1 2 3 4 5

Now, let’s apply the $-operator to our data frame:

data$a                              # Applying $-operator to data frame
# 1

Works perfectly fine!

Next, I’ll explain why this error message occurred. So if you want to avoid such errors in future, keep on reading!


Explanation for Error Message “$ operator is invalid for atomic vectors”

In the previous examples, we have discussed the error message “$ operator is invalid for atomic vectors”. This tells us that we shouldn’t use the $-operator for atomic vectors.

But what are atomic vectors?! Atomic vectors are typically one-dimensional data objects that are created by the c() or the vector() functions. You can learn more about the properties of atomic vectors here.

So how can we check whether a data object is an atomic vector? For this, we can apply the is.atomic function. Let’s use the is.atomic function for our vector object that we have created in Example 1:

is.atomic(vec)                      # Apply is.atomic function to vector

The RStudio console returns the logical value TRUE, i.e. our data object vec is an atomic vector.

As you can see in the help documentation of the $-operator (i.e. ?”$”), the $-operator can only be applied to recursive objects. You can find more about the difference of atomic vectors and recursive objects here.

In R, we can use the is.recursive function to check whether our data object is recursive:

is.recursive(vec)                   # Apply is.recursive function to vector

Our object vec is not recursive!

Let’s apply the is.atomic and is.recursive functions to our data frame:

is.atomic(data)                     # Apply is.atomic function to data

The data frame is not atomic…

is.recursive(data)                  # Apply is.recursive function to data

…but it is recursive.

For that reason, we can use the $-operator for our data frame.

You might use the is.atomic and is.recursive functions in future, whenever you are facing the error message “$ operator is invalid for atomic vectors”.


Video & Further Resources

Have a look at the following video which I have published on my YouTube channel. In the video, I show the examples of this article:



Also, you may have a look at the other tutorials on www.statisticsglobe.com:


Summary: In this R tutorial you learned how to fix $-operator errors. Please let me know in the comments, if you have additional questions and/or comments. Besides that, don’t forget to subscribe to my email newsletter to get updates on new articles.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

6 Comments. Leave new

  • Hi! I would need to find the maximum and minimum value of a dicom image. I use which.max.image and which.min.image function but I get an error message. Can you help me?

  • This is Very informative post. I really liked your all the added stuff and I would love to recommend to all my connections who also face this error of $ operator is invalid for atomic vectors.
    Also, few days ago i also face this error and was worried about it. I was curious to solve this issue and that moment i start to find solutions for this problem on internet. And found a tech blog page keepthetech.com explaining issue of this problem and methods to solve this issue.
    Thank you so much for posting and showing your expertise to help everyone.
    Keep up the good work!

  • Hello,
    Thank you for sharing on what to do when such error occurs. For my case the x vector is recursive but i still get the same error. Please see below. This is just part of the code. The x vector is a list of 6 elements

    So this x vector is under the function2 loop that is called when executing the OP1 command.

    > maxit start OP1 is.atomic(x)
    [1] FALSE
    > is.recursive(x)
    [1] TRUE
    > View(x)

    Any feedback will be appreciated.

    many thanks.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.