[英]No dimensions of non-empty numeric vector in R
I have a problem with my numeric vector and dim()
in R. I want to know the dimensions of my vector X with: 我的数字向量和R中的
dim()
有问题。我想知道向量X的尺寸:
dim(X)
However, that function returns NULL. 但是,该函数返回NULL。
If I type: 如果输入:
X
I can see that the X is not empty. 我可以看到X不为空。 Why does dim or nrow report it as "NULL"?
为何暗淡或nrow将其报告为“ NULL”?
Part of X:
[93486] 6.343e-01 6.343e-01 6.343e-01 6.343e-01 6.343e-01 6.343e-01 6.346e-01
[93493] 6.346e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01
[93500] 6.347e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01
[93507] 6.347e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01
[93514] 6.347e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01
[93521] 6.347e-01 6.347e-01 6.347e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01
[93528] 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01
[93535] 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01
[93542] 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01
[93549] 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01
[93556] 6.348e-01 6.348e-01 6.349e-01 6.349e-01 6.349e-01 6.349e-01 6.349e-01
[93563] 6.349e-01 6.349e-01 6.349e-01 6.349e-01 6.349e-01 6.349e-01 6.349e-01
[93570] 6.349e-01 6.349e-01 6.349e-01 6.349e-01 6.349e-01 6.349e-01 6.349e-01
> dim(X)
NULL
> class(X)
[1] "numeric"
> nrow(pvals_vector)
NULL
Why is there no dimensions of X? 为什么没有X的尺寸?
Because it is a one-dimensional vector. 因为它是一维向量。 It has length.
它有长度。 Dimensions are extra attributes applied to a vector to turn it into a matrix or a higher dimensional array:
维度是应用于矢量的额外属性,可将其转换为矩阵或更高维的数组:
x <- 1:6
dim( x )
#NULL
length( x )
#[1] 6
dim( matrix( x , 2 , 3 ) )
#[1] 2 3
As a side note, I wrote a function which returns length
if dim==NULL
: 作为附带说明,我编写了一个函数,如果
dim==NULL
,则返回length
:
I rewrote this function so it doesn't foul up calls to base::dim
inside any existing functions. 我重写了此函数,以便它不会
base::dim
任何现有函数中对base::dim
调用。
# return dim() when it's sensible and length() elsewise
# let's not allow multiple inputs, just like base::dim, base::length
# Interesting fact -- the function "dim" and the function " dim<-" are different
# primitives, so this function here doesn't interfere with the latter.
dim <- function(item) {
if (is.null(base::dim(item)) ) {
dims<-length(item)
} else{
dims <- base::dim(item)
}
return(dims)
}
Below is the original posted code 以下是原始发布的代码
function(items) {
dims<-vector('list',length(items))
names(dims)<-items
for(thing in seq(1,length(items))) {
if (is.null(dim(get(items[thing])))) {
dims[[thing]]<-length(get(items[thing]))
} else{
#load with dim()
dims[[thing]]<-dim(get(items[thing]))
}
}
return(dims)
}
Or, as SimonO pointed out, you can "force" a 1xN matrix if desired. 或者,正如SimonO指出的,如果需要,您可以“强制”使用1xN矩阵。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.