The number of data points in matrix and vector forms

后端 未结 1 1342
时光取名叫无心
时光取名叫无心 2021-01-23 03:41

Supposed that X contains 1000 rows with m columns, where m equal to 3 as follows:

set.seed(5)  
X <- cbind(rnorm(1000         


        
1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-23 04:25

    You can use NROW for both cases. From ?NROW

    nrow and ncol return the number of rows or columns present in x. NCOL and NROW do the same treating a vector as 1-column matrix.

    So that means that even if the subset is dropped down to a vector, as long as x is an array, vector, or data frame NROW will treat it as a one-column matrix.

    sub1 <- X[,2:3]
    is.matrix(sub1)
    # [1] TRUE
    NROW(sub1)
    # [1] 1000
    sub2 <- X[,1]
    is.matrix(sub2)
    # [1] FALSE
    NROW(sub2)
    # [1] 1000
    

    So if(NROW(X) < 1000L) a + b should work regardless of whether X is a matrix or a vector. I use <= below, since X has exactly 1000 rows in your example.

    a <- 5; b <- 15
    if(NROW(sub1) <= 1000L) a + b
    # [1] 20
    if(NROW(sub2) <= 1000L) a + b
    # [1] 20
    

    A second option would be to use drop=FALSE when you make the variable selection. This will make the subset remain a matrix when the subset is only one column. This way you can use nrow with no worry. An example of this is

    X[, 1, drop = FALSE]
    

    0 讨论(0)
提交回复
热议问题