【问题标题】:How to create a binary matrix of inventory per row? (R)如何创建每行库存的二进制矩阵? (右)
【发布时间】:2012-02-17 09:29:21
【问题描述】:

我有一个包含 9 列的数据框,其中包含一系列因素。每行可以填充所有 9 列(因为该行包含 9 个“东西”),但大多数没有(大多数在 3-4 之间)。列也不是特定的,就像项目 200 显示在第 1 列和第 3 列中一样,它是同一件事。我想为包含所有因素的每一行创建一个二进制矩阵。

Ex(缩短为 4 列只是为了说明问题)

R1 3  4   5   8
R2 4  6   7   NA
R3 1  5  NA   NA
R4 2  6   8   9

应该变成

     1  2  3  4  5  6  7  8  9 
r1   0  0  1  1  1  0  0  1  0
r2   0  0  0  1  0  1  1  0  0
r3   1  0  0  0  1  0  0  0  0
r4   0  1  0  0  0  1  0  1  1

我研究了 writeBin/readBin、K-clustering(这是我想做的事情,但我需要先摆脱 NA)、模糊聚类、标签聚类。只是有点迷失方向。

我尝试编写两个 for 循环,按列/行从矩阵中提取数据,然后将 0 和 1 分别保存在新矩阵中,但我认为存在范围问题。

你们是最棒的。谢谢!

【问题讨论】:

    标签: r sparse-matrix cluster-analysis


    【解决方案1】:

    这些都是很好的答案。我想我会贡献我写的原始解决方案,我的一个朋友修改后可以实际工作。

    for(i in seq(nrow(x)))
      for(j in seq(ncol(x)))
      if(!is.na(x[i,j])) { y[i, x[i,j]] = 1 }
    

    设置一些较早的参数后,两个 for 循环可以工作,但速度非常慢。看起来这些其他解决方案的工作速度要快得多!

    【讨论】:

      【解决方案2】:

      这是一个基本的 R 解决方案:

      # Read in the data, and convert to matrix form
      df <- read.table(text = "
      3  4   5   8
      4  6   7   NA
      1  5  NA   NA
      2  6   8   9", header = FALSE)
      m <- as.matrix(df)
      
      # Create a two column matrix containing row/column indices of cells to be filled 
      # with 'one's
      id <- cbind(rowid = as.vector(t(row(m))), 
                  colid = as.vector(t(m)))
      id <- id[complete.cases(id), ]
      
      # Create output matrix
      out <-  matrix(0, nrow = nrow(m), ncol = max(m, na.rm = TRUE))
      out[id] <- 1
      #      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
      # [1,]    0    0    1    1    1    0    0    1    0
      # [2,]    0    0    0    1    0    1    1    0    0
      # [3,]    1    0    0    0    1    0    0    0    0
      # [4,]    0    1    0    0    0    1    0    1    1
      

      【讨论】:

      • 乔希,这令人印象深刻。这有什么术语吗?我在想库存矩阵、项目矩阵或二元矩阵,但这些似乎都与其他想法有关。
      • 谢谢。我有点认为结果是存在/不存在矩阵的指示矩阵(在indicator function之后)(因为它编码每个项目在给定行中是否存在或不存在) .不过,不确定它是否有公认的通用名称。
      • 这应该是“指标矩阵......或存在/不存在矩阵”(不是“OF”)。编辑评论本身为时已晚。
      【解决方案3】:

      这应该可以解决问题:

      # The Incantation
      options(stringsAsFactors = FALSE)
      
      library(reshape2)
      
      # Your example data
      dat <- data.frame(id = c("R1", "R2", "R3", "R4"),
                        col1 = c(3, 4, 1, 2),
                        col2 = c(4, 6, 5, 6),
                        col3 = c(5, 7, NA, 7),
                        col4 = c(8, NA, NA, 9)
      )
      
      # Melt it down
      dat.melt <- melt(dat, id.var = "id")
      
      # Cast it back out, with the row IDs remaining the row IDs
      # and the values of the columns becoming the columns themselves.
      # dcast() will default to length to aggregate records - which means
      # that the values in this data.frame are a count of how many times
      # each value occurs in each row's columns (which, based on this data,
      # seems to be capped at just once).
      dat.cast <- dcast(dat.melt, id ~ value)
      

      结果:

      dat.cast
        id 1 2 3 4 5 6 7 8 9 NA
      1 R1 0 0 1 1 1 0 0 1 0  0
      2 R2 0 0 0 1 0 1 1 0 0  1
      3 R3 1 0 0 0 1 0 0 0 0  2
      4 R4 0 1 0 0 0 1 1 0 1  0
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-27
        • 2014-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-04
        相关资源
        最近更新 更多