【问题标题】:No dimensions of non-empty numeric vector in RR中没有非空数值向量的维度
【发布时间】:2013-08-31 04:23:21
【问题描述】:

我的数字向量和 R 中的 dim() 有问题。我想知道向量 X 的尺寸:

dim(X)

但是,该函数返回 NULL。

如果我输入:

X

我可以看到 X 不是空的。为什么 dim 或 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

为什么没有X的维度?

【问题讨论】:

    标签: r vector numeric


    【解决方案1】:

    因为它是一个一维向量。它有长度。维度是应用于向量以将其转换为矩阵或更高维数组的额外属性:

    x <- 1:6
    dim( x )
    #NULL
    
    length( x )
    #[1] 6
    
    dim( matrix( x , 2 , 3 ) )
    #[1] 2 3
    

    【讨论】:

    • 那我有问题了。我想给我的向量的行命名,如下所示: > rownames(X) rownames<-(*tmp*, value = c(35108L, 41354L, 142094L, 5816L, : 尝试设置没有尺寸的对象上的“行名”由于我的对象没有尺寸而无法设置行名?
    • 这样做:x &lt;- matrix( x , length(x) , 1 ); rownames(x) &lt;- whatever 这将为您提供一个可以应用行名的单列矩阵。或者只是做例如:names(x) &lt;- whatever,如果你只想要一个命名向量。
    • @user1261558 太好了!此外,由于您在这里相对较新,您可能想阅读aboutfaq 以了解 SO 的工作原理。如果当您收到解决问题的答案时,您可以通过单击小复选标记或投票一个有用的答案来接受它,那么 StackOverflow 对每个人都会变得更有价值。您绝对没有义务这样做,但如果答案确实解决了您的问题,这是“回馈”网站的好方法。谢谢!
    【解决方案2】:

    作为旁注,我写了一个函数,如果 dim==NULL 则返回 length

    2019 年 6 月编辑:

    我重写了这个函数,这样它就不会在任何现有函数中破坏对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)
        }
    

    下面是原贴代码

    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)
            }
    

    或者,正如 SimonO 指出的那样,如果需要,您可以“强制”一个 1xN 矩阵。

    【讨论】:

      猜你喜欢
      • 2019-02-21
      • 2018-12-17
      • 2021-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-03
      • 2012-04-12
      • 2014-09-01
      相关资源
      最近更新 更多