R table by matrix row

后端 未结 4 1339
一向
一向 2020-12-31 06:01

For a matrix (as.matrix), how can I generate a table where rows are equal to rows of the matrix?

>table(matrix)

and

&g         


        
4条回答
  •  死守一世寂寞
    2020-12-31 06:36

    ## source data
    x=as.matrix(read.table(text="
       1  2  3  4 
    a  5  5  4  6    
    b  5  5  5  5     
    c  8  7  6  6   
    d  2  6  6  6     
    e  7  7  5  4
    "))
    
    # result
    
    table(rep(rownames(x),ncol(x)),c(x))
    
    #   2 4 5 6 7 8
    # a 0 1 2 1 0 0
    # b 0 0 4 0 0 0
    # c 0 0 0 2 1 1
    # d 1 0 0 3 0 0
    # e 0 1 1 0 2 0
    

提交回复
热议问题