$ Operator Is Invalid for Atomic Vectors Error in R

  1. the $ operator is invalid for atomic vectors Error in R
  2. the [] and [[]] Operators in R
  3. Use the getElement() Function
  4. Convert a Vector to a Data Frame and Use the $ Operator

R provides various ways in which we can extract elements of a vector. This article will outline the facilities of base R for this purpose.

An important point to note is that one of R’s extract operators, $, does not work for vectors. We will begin with this and then see the different options available.

the $ operator is invalid for atomic vectors Error in R

The $ operator can be used to extract named columns of a data frame or named elements of lists, but it does not work on vectors. R raises an error.

The $ operator needs a name, not a numeric index. So we will create a vector in which the elements are named.

Example Code:

# Create a vector using c(). Assign names for its elements.
vr1 = c(a = "alpha", d = "delta", r = "rho", s = "sigma")
vr1

# Try to extract the second element.
vr1$d

Output:

> vr1$d
Error in vr1$d : $ operator is invalid for atomic vectors

R tells us that the $ operator is invalid for atomic vectors.

A vector of numbers or strings is called atomic because all elements are of the same type, and it cannot have another vector as its element.

For atomic vectors like the one in our example, we need to use R’s other facilities to extract elements.

the [] and [[]] Operators in R

We can use [] and [[]] operators to extract elements of vectors.

  1. [] and [[]] can use position indices or names.
  2. [] can extract multiple elements, while [[]] can only extract one element.
  3. [[]] drops the name, while [] keeps the name when extracting an element.

Example Code:

# Use [] with index number or name to extract a single element.
vr1[2]
vr1['d']

# Use [[]] with the index number of the name to extract a single element.
vr1[[2]]
vr1[['d']]

# Use [] to extract multiple elements. Use indices or names.
vr1[c(2,4)]
vr1[c('d', 's')]

Output:

> # Use [] with index number or name to extract a single element.
> vr1[2]
      d
"delta"
> vr1['d']
      d
"delta"

> # Use [[]] with index number of name to extract a single element.
> vr1[[2]]
[1] "delta"
> vr1[['d']]
[1] "delta"

> # Use [] to extract multiple elements. Use indices or names.
> vr1[c(2,4)]
      d       s
"delta" "sigma"
> vr1[c('d', 's')]
      d       s
"delta" "sigma"

Use the getElement() Function

Besides the extract operators, R also provides the getElement() function to extract elements from vectors.

The getElement() is similar to the [[]] operator with the option exact = TRUE.

  1. Only one element can be selected using the name or index.
  2. The name is dropped.

Example Code:

getElement(vr1, 'd')
getElement(vr1, 2)

Output:

> getElement(vr1, 'd')
[1] "delta"
> getElement(vr1, 2)
[1] "delta"

Convert a Vector to a Data Frame and Use the $ Operator

If we want to use the $ operator to extract an element from a vector, we first need to convert the vector to a data frame.

Example Code:

dfr1 = as.data.frame(vr1)
dfr1

Output:

> dfr1 = as.data.frame(vr1)
> dfr1
    vr1
a alpha
d delta
r   rho
s sigma

We have a data frame with one column and four rows. The name of the vector has become the column name; the element names have become row names.

We need to specify the desired row and column names to extract an element of the original vector. We can also specify the row name for all columns.

Example Code:

# Use names for row and column.
dfr1['d','vr1']

# Only name the row, and select all columns.
dfr1['d',]

Output:

> # Use names for row and column.
> dfr1['d','vr1']
[1] "delta"
> # Only name the row and select all columns.
> dfr1['d',]
[1] "delta"

Related Article - R Error

  • Use the Predict Function on a Linear Regression Model in R