0

我有多个带有大矩阵的 .csv 文件,大约 300 行和 2000 列。我想通过选择具有相等值为 1 的整个列来为每一行开发一个新的矩阵表。我想保留行名和列名,并想在目录中创建具有行名的文件。

这是数据集的示例:

       pom.xml. ZooKeeper.java HBase.java Hadoop.java. BasicServer.java. Abstract.java. HBaseRegion.java
WHIRR-25        1              0          1            0                 1              1                1
WHIRR-28        1              0          1            0                 0              1                0
WHIRR-55        0              0          1            0                 0              0                0
WHIRR-61        0              0          0            0                 0              1                0
WHIRR-76        0              0          1            0                 0              0                0
WHIRR-87        1              1          1            0                 0              1                1
WHIRR-92        1              0          0            1                 0              1                1

所以这个数据集将产生如下输出:

    Whirr-25
                   pom.xml. ZooKeeper.java HBase.java Hadoop.java. BasicServer.java. Abstract.java. HBaseRegion.java
 WHIRR-25        1              0          1            0                 1              1                1
 WHIRR-28        1              0          1            0                 0              1                0
 WHIRR-55        0              0          1            0                 0              0                0
 WHIRR-61        0              0          0            0                 0              1                0
 WHIRR-76        0              0          1            0                 0              0                0
 WHIRR-87        1              1          1            0                 0              1                1
 WHIRR-92        1              0          0            1                 0              1                1

Whirr-28
                pom.xml. ZooKeeper.java HBase.java Hadoop.java. BasicServer.java. Abstract.java. HBaseRegion.java
 WHIRR-28        1              0          1            0                 0              1                0
 WHIRR-55        0              0          1            0                 0              0                0
 WHIRR-61        0              0          0            0                 0              1                0
 WHIRR-76        0              0          1            0                 0              0                0
 WHIRR-87        1              1          1            0                 0              1                1
 WHIRR-92        1              0          0            1                 0              1                1

Whirr-55

             pom.xml. ZooKeeper.java HBase.java Hadoop.java. BasicServer.java. Abstract.java. HBaseRegion.java

WHIRR-55        0              0          1            0                 0              0                0
WHIRR-76        0              0          1            0                 0              0                0
WHIRR-87        1              1          1            0                 0              1                1

Whirr-61
          pom.xml. ZooKeeper.java HBase.java Hadoop.java. BasicServer.java. Abstract.java. HBaseRegion.java
WHIRR-61        0              0          0            0                 0              1                0
WHIRR-87        1              1          1            0                 0              1                1
WHIRR-92        1              0          0            1                 0              1                1

Whirr-76
               pom.xml. ZooKeeper.java HBase.java Hadoop.java. BasicServer.java. Abstract.java. HBaseRegion.java
WHIRR-76        0              0          1            0                 0              0                0
WHIRR-87        1              1          1            0                 0              1                1

Whirr-87
              pom.xml. ZooKeeper.java HBase.java Hadoop.java. BasicServer.java. Abstract.java. HBaseRegion.java
WHIRR-87        1              1          1            0                 0              1                1
WHIRR-92        1              0          0            1                 0              1                1

Whirr-92
              pom.xml. ZooKeeper.java HBase.java Hadoop.java. BasicServer.java. Abstract.java. HBaseRegion.java
 WHIRR-92        1              0          0            1                 0              1                1

我应用了这个脚本,但脚本只基于列而不是行创建新表:

 dat <- read.table(file="Task_vs_Files_Proj.csv", header=T, sep=",", row.names=1) 
    dat

    apply( sapply(dat , function(x) return( as.logical(x) ) ), 2, function(x) dat[x, ])

$pom.xml.
        pom.xml. ZooKeeper.java HBase.java Hadoop.java. BasicServer.java. Abstract.java. HBaseRegion.java
    WHIRR-25        1              0          1            0                 1              1                1
    WHIRR-28        1              0          1            0                 0              1                0
    WHIRR-87        1              1          1            0                 0              1                1
    WHIRR-92        1              0          0            1                 0              1                1

    $ZooKeeper.java
             pom.xml. ZooKeeper.java HBase.java Hadoop.java. BasicServer.java. Abstract.java. HBaseRegion.java
    WHIRR-87        1              1          1            0                 0              1                1

    $HBase.java
             pom.xml. ZooKeeper.java HBase.java Hadoop.java. BasicServer.java. Abstract.java. HBaseRegion.java
    WHIRR-25        1              0          1            0                 1              1                1
    WHIRR-28        1              0          1            0                 0              1                0
    WHIRR-55        0              0          1            0                 0              0                0
    WHIRR-76        0              0          1            0                 0              0                0
    WHIRR-87        1              1          1            0                 0              1                1

在这里感谢专家的帮助...谢谢

4

1 回答 1

1

据我所知,你想要

# cycle through all rows
for(which_row in seq_len(nrow(.data))){
  # get the subset of the rows from this row 
  subset_data <- .data[which_row:nrow(.data),]
  # which elements for each column == 1
  which_one <- lapply(subset_data, function(x){which(as.logical(x))})
  # drop the columns where there are no 1's
  which_one <- Filter(function(x){length(x) >0},which_one)
  # filter to those which == 1, and then get the unique combination
  # of rows (sorted to original order)
  which_rows <- sort(Reduce(union,Filter(function(x) {1 %in% x}, which_one)))
  # the file name
  file_name <- sprintf('file_%s.csv', row.names(.data)[which_row])
  # save
  write.csv(subset_data[which_rows,], file_name, row.names = T)
  # prints the data set to the console for checking
  print(subset_data[which_rows,])
  # message to show what file is created
  message(sprintf('Saving %s', file_name))
}
于 2012-09-20T01:26:55.280 回答