【问题标题】:R table by matrix rowR表按矩阵行
【发布时间】:2013-04-28 04:15:01
【问题描述】:

对于矩阵(as.matrix),如何生成行数等于矩阵行数的表?

>table(matrix)

>hist(matrix)

显示矩阵中每个唯一数据值的累积和,但我想要一个表格,其中行与每个矩阵行的值相同,表格列是矩阵中每个唯一数据值的总和。

示例矩阵:

   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      

所需的输出表:

   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

【问题讨论】:

  • 有两个比我的更高效、更优雅。您可能需要考虑从我的答案中删除复选标记并将其放在其他答案之一上(例如 @GregoryDe​​min 的)

标签: r matrix


【解决方案1】:

另一种方法是将matrix 转换为长data.frame(使用stack),此时您可以轻松使用table

这是您的数据:

mymat <- structure(c(5L, 5L, 8L, 2L, 7L, 5L, 5L, 7L, 6L, 7L, 4L, 5L, 6L, 
            6L, 5L, 6L, 5L, 6L, 6L, 4L), .Dim = c(5L, 4L), .Dimnames = list(
              c("a", "b", "c", "d", "e"), c("1", "2", "3", "4")))

这是长data.frame的样子:

head(stack(data.frame(t(mymat))))
#   values ind
# 1      5   a
# 2      5   a
# 3      4   a
# 4      6   a
# 5      5   b
# 6      5   b

以下是我们如何使用它来创建您想要的表格:

with(stack(data.frame(t(mymat))), table(ind, values))
#    values
# ind 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

【讨论】:

    【解决方案2】:
    ## 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
    

    【讨论】:

      【解决方案3】:

      我也用过apply

      t(apply(mat, 1, function(x) table(factor(x, levels = unique(sort(c(mat)))))))
      
      R > mat  = matrix(sample(1:8, 20, replace = T), 5, 4)
      R > mat
           [,1] [,2] [,3] [,4]
      [1,]    5    6    1    4
      [2,]    4    3    4    8
      [3,]    4    8    4    3
      [4,]    3    3    5    1
      [5,]    1    1    3    1
      R > t(apply(mat, 1, function(x) table(factor(x, levels = unique(sort(c(mat)))))))
           1 3 4 5 6 8
      [1,] 1 0 1 1 1 0
      [2,] 0 1 2 0 0 1
      [3,] 0 1 2 0 0 1
      [4,] 1 2 0 1 0 0
      [5,] 3 1 0 0 0 0
      

      【讨论】:

        【解决方案4】:

        您可以在行上使用apply,然后使用mapplyifelse 语句来取回您的矩阵。

        假设X 是你的矩阵:

        # this will get you the values, just not in a nice matrix
        tables.list <- apply(X, 1, table)
        
        # unique values
        vals <- sort(unique(c(X)))
        
        # this will get you the matrix
        results <- t(mapply(function(v, t)
           ifelse(v %in% names(t), t[as.character(v)], 0), list(vals), tables.list ))
        
        # give it names
        dimnames(results) <- list(rownames(X), vals)
        
        results
        
        #   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
        

        【讨论】:

          猜你喜欢
          • 2016-08-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-26
          • 2013-04-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多