# 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 # TRUE```

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 # FALSE```

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 # FALSE```

The data frame is not atomic…

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

…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

• Alessandra
March 25, 2021 11:15 am

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?