I want to compute the mean of "Population" of built-in matrix state.x77
. The codes are :
我想计算内建矩阵的“总体”的均值。代码是:
apply(state.x77[,"Population"],2,FUN=mean)
#Error in apply(state.x77[, "Population"], 2, FUN = mean) :
# dim(X) must have a positive length
how can I prevent this error? If I use $
sign
我怎样才能避免这个错误呢?如果我使用$符号。
apply(state.x77$Population,2,mean)
# Error in state.x77$Population : $ operator is invalid for atomic vectors
What is atomic vector?
原子的矢量是什么?
1 个解决方案
#1
29
To expand on joran's comments, consider:
若要扩展joran的评论,请考虑:
> is.vector(state.x77[,"Population"])
[1] TRUE
> is.matrix(state.x77[,"Population"])
[1] FALSE
So, your Population
data is now no diferent from any other vector, like 1:10
, which has neither columns or rows to apply
against. It is just a series of numbers with no more advanced structure or dimension. E.g.
所以,你的人口数据现在和其他任何向量都没有区别,比如1:10,它既没有列也没有行来反对。它只是一系列没有更高级的结构或维度的数字。如。
> apply(1:10,2,mean)
Error in apply(1:10, 2, mean) : dim(X) must have a positive length
Which means you can just use the mean
function directly against the matrix subset which you have selected: E.g.:
也就是说,你可以直接用平均函数来对抗你选择的矩阵子集:例如:
> mean(1:10)
[1] 5.5
> mean(state.x77[,"Population"])
[1] 4246.42
To explain 'atomic' vector more, see the R FAQ again (and this gets a bit complex, so hold on to your hat)...
为了更详细地解释“原子”矢量,请再次查看R FAQ(这有点复杂,所以请坚持你的帽子)……
R has six basic (‘atomic’) vector types: logical, integer, real, complex, string (or character) and raw. http://cran.r-project.org/doc/manuals/r-release/R-lang.html#Vector-objects
R有六个基本的(“原子”)向量类型:逻辑、整数、实数、复杂、字符串(或字符)和raw。http://cran.r-project.org/doc/manuals/r-release/R-lang.html矢量对象
So atomic in this instance is referring to vectors as the basic building blocks of R objects (like atoms make up everything in the real world).
在这个例子中,原子是指向量作为R对象的基本构件(就像原子构成了现实世界中的一切)。
If you read R's inline help by entering ?"$"
as a command, you will find it says:
如果你通过输入“$”来读取R的内联帮助,你会发现它说:
‘$’ is only valid for recursive objects, and is only discussed in the section below on recursive objects.
“$”只对递归对象有效,并且只在递归对象上的节中讨论。
Since vectors (like 1:10
) are basic building blocks ("atomic"), with no recursive sub-elements, trying to use $
to access parts of them will not work.
由于向量(如1:10)是基本的构建块(“原子”),没有递归子元素,因此试图使用$来访问它们的部分将不起作用。
Since your matrix (statex.77
) is essentially just a vector with some dimensions, like:
因为你的矩阵(statex.77)本质上就是一个具有某些维度的向量,比如:
> str(matrix(1:10,nrow=2))
int [1:2, 1:5] 1 2 3 4 5 6 7 8 9 10
...you also can't use $
to access sub-parts.
…您也不能使用$访问子部分。
> state.x77$Population
Error in state.x77$Population : $ operator is invalid for atomic vectors
But you can access subparts using [
and names like so:
但是您可以使用[和这样的名称访问子部件:
> state.x77[,"Population"]
Alabama Alaska Arizona...
3615 365 2212...
#1
29
To expand on joran's comments, consider:
若要扩展joran的评论,请考虑:
> is.vector(state.x77[,"Population"])
[1] TRUE
> is.matrix(state.x77[,"Population"])
[1] FALSE
So, your Population
data is now no diferent from any other vector, like 1:10
, which has neither columns or rows to apply
against. It is just a series of numbers with no more advanced structure or dimension. E.g.
所以,你的人口数据现在和其他任何向量都没有区别,比如1:10,它既没有列也没有行来反对。它只是一系列没有更高级的结构或维度的数字。如。
> apply(1:10,2,mean)
Error in apply(1:10, 2, mean) : dim(X) must have a positive length
Which means you can just use the mean
function directly against the matrix subset which you have selected: E.g.:
也就是说,你可以直接用平均函数来对抗你选择的矩阵子集:例如:
> mean(1:10)
[1] 5.5
> mean(state.x77[,"Population"])
[1] 4246.42
To explain 'atomic' vector more, see the R FAQ again (and this gets a bit complex, so hold on to your hat)...
为了更详细地解释“原子”矢量,请再次查看R FAQ(这有点复杂,所以请坚持你的帽子)……
R has six basic (‘atomic’) vector types: logical, integer, real, complex, string (or character) and raw. http://cran.r-project.org/doc/manuals/r-release/R-lang.html#Vector-objects
R有六个基本的(“原子”)向量类型:逻辑、整数、实数、复杂、字符串(或字符)和raw。http://cran.r-project.org/doc/manuals/r-release/R-lang.html矢量对象
So atomic in this instance is referring to vectors as the basic building blocks of R objects (like atoms make up everything in the real world).
在这个例子中,原子是指向量作为R对象的基本构件(就像原子构成了现实世界中的一切)。
If you read R's inline help by entering ?"$"
as a command, you will find it says:
如果你通过输入“$”来读取R的内联帮助,你会发现它说:
‘$’ is only valid for recursive objects, and is only discussed in the section below on recursive objects.
“$”只对递归对象有效,并且只在递归对象上的节中讨论。
Since vectors (like 1:10
) are basic building blocks ("atomic"), with no recursive sub-elements, trying to use $
to access parts of them will not work.
由于向量(如1:10)是基本的构建块(“原子”),没有递归子元素,因此试图使用$来访问它们的部分将不起作用。
Since your matrix (statex.77
) is essentially just a vector with some dimensions, like:
因为你的矩阵(statex.77)本质上就是一个具有某些维度的向量,比如:
> str(matrix(1:10,nrow=2))
int [1:2, 1:5] 1 2 3 4 5 6 7 8 9 10
...you also can't use $
to access sub-parts.
…您也不能使用$访问子部分。
> state.x77$Population
Error in state.x77$Population : $ operator is invalid for atomic vectors
But you can access subparts using [
and names like so:
但是您可以使用[和这样的名称访问子部件:
> state.x77[,"Population"]
Alabama Alaska Arizona...
3615 365 2212...