如何在 R 中修复:$运算符对原子向量无效
在本文中,我们将看到如何修复 $运算符对 R 编程语言中的原子向量无效。
在 R 中可能面临的错误是:
$ operator is invalid for atomic vectors
当我们尝试使用 $运算符获取原子向量的元素时,R 编译器会产生这样的错误。原子向量只是一个包含借助 c() 和 vector() 函数创建的数据的一维对象。 R 不允许我们使用 $ 符号访问原子向量的元素。但是我们可以使用双括号,即 [[]] 或 getElement()函数来访问它们。
何时可能发生此错误
让我们考虑一个例子,其中我们有一个数字向量vect,用前五个自然数初始化。使用 R 中的 names()函数为每个数字分配一个名称。 names()函数的语法如下所示:
Syntax: names(vect) <- value
Parameters:
- vect: an Object i.e. vector, matrix, data frame, etc.
- value: The corresponding names to be assigned
R
# Define a vector
vect <- c(1, 2, 3, 4, 5)
# Set integers names
names(vect) <- c('first', 'second', 'third',
'fourth', 'fifth')
# Display the vector
vect
R
# Define a vector
vect <- c(1, 2, 3, 4, 5)
# Set integers names
names(vect) <- c('first', 'second', 'third',
'fourth', 'fifth')
# Display the vector
vect$third
R
# Define a vector
vect <- c(1, 2, 3, 4, 5)
# Set integers names
names(vect) <- c('first', 'second', 'third',
'fourth', 'fifth')
# Display the third element of the
# vector
vect[['third']]
R
# Define a vector
vect <- c(1, 2, 3, 4, 5)
# Set integers names
names(vect) <- c('first', 'second', 'third',
'fourth', 'fifth')
# Display the third element of
# the vector
getElement(vect, 'third')
R
# Define a vector
vect <- c(1, 2, 3, 4, 5)
# Set integers names
names(vect) <- c('first', 'second', 'third',
'fourth', 'fifth')
# Transform the vector to data frame
dataframe <- as.data.frame(t(vect))
# Access the third element
dataframe$third
输出:

现在让我们尝试使用语句vect$third 访问元素第三个元素:
R
# Define a vector
vect <- c(1, 2, 3, 4, 5)
# Set integers names
names(vect) <- c('first', 'second', 'third',
'fourth', 'fifth')
# Display the vector
vect$third
输出:

R 编译器会产生错误,因为我们不允许以这种方式访问原子向量中的元素。要检查向量是否真的是原子向量,我们可以使用 R 中的 is.atomic()函数。该函数的语法如下:
Syntax: is.atomic(vect)
Parameters:
vect: Here, vect represents a vector
Return Type:
- TRUE: If vect is an atomic vector
- FALSE: If vect is not an atomic vector
如何修复错误
有三种使用方式 我们可以修复这个错误:
方法一:使用双括号访问元素
我们可以在双括号 [[]] 的帮助下轻松访问原子向量的元素:
R
# Define a vector
vect <- c(1, 2, 3, 4, 5)
# Set integers names
names(vect) <- c('first', 'second', 'third',
'fourth', 'fifth')
# Display the third element of the
# vector
vect[['third']]
输出:

方法2:使用 getElement()函数访问元素
另一种方法是使用 getElement()函数来访问原子向量的元素。该函数具有以下语法:
Syntax: getElement(vect, specifier)
Parameters:
- vect: It represents a vector
- specifier: It represent the name of the element that we want to access
Return Type:
- Returns an element marked with specifier
R
# Define a vector
vect <- c(1, 2, 3, 4, 5)
# Set integers names
names(vect) <- c('first', 'second', 'third',
'fourth', 'fifth')
# Display the third element of
# the vector
getElement(vect, 'third')
输出:

方法 3:通过将向量转换为数据框 & 然后使用 $运算符来访问元素
另一种方法是先将向量转换为数据框,然后应用 $运算符。我们可以使用 as.data.frame()函数将向量转换为数据框。该函数的语法如下:
Syntax: as.data.frame(object)
Parameter:
- object represents a vector or matrix
Return Type:
- Returns a vector
R
# Define a vector
vect <- c(1, 2, 3, 4, 5)
# Set integers names
names(vect) <- c('first', 'second', 'third',
'fourth', 'fifth')
# Transform the vector to data frame
dataframe <- as.data.frame(t(vect))
# Access the third element
dataframe$third
输出:
