R class of subset of matrix and data.frame

a = matrix(
    c(2, 4, 3, 1, 5, 7), # the data elements
    nrow=2,              # number of rows
    ncol=3,              # number of columns
    byrow = TRUE)        # fill matrix by rows
    
class( (a[1,])
[1] "numeric"

class(a[c(1,2),])
[1] "matrix"

class(as.matrix(a[1,]))
[1] "matrix"
#### a row changed to be a column ????

matrix(a[ 1, ],ncol=3) #
 

  n = c(2, 3, 5)
  s = c("aa", "bb", "cc")
  b = c(TRUE, FALSE, TRUE)
  df = data.frame(n, s, b)


class( df[1,])
[1] "data.frame"

class(df[c(1,2),])
[1] "data.frame"

class(as.matrix(df[1,]))
[1] "matrix"

猜你喜欢

转载自www.cnblogs.com/emanlee/p/9086200.html