R and matrix with 1 row

后端 未结 2 1774
臣服心动
臣服心动 2020-12-06 21:18

is it possible to have a matrix with 1 row only in R?

Here is my code:

   nas <- which(!is.na(y))   
   x <- x[nas,]
   y <- y[nas]

   ...
         


        
2条回答
  •  悲&欢浪女
    2020-12-06 21:50

    You need to specify drop = FALSE to stop R coercing a matrix or array to the lowest possible number of dimensions. See ?`[` for more details.

    x <- matrix(1:4,ncol=2)
    x[1,]
    ## [1] 1 2
    x[1,,drop=F]
    ##      [,1] [,2]
    ## [1,]    1    3
    

提交回复
热议问题