简体   繁体   English

如何从二进制表/矩阵创建表达式输入样式格式?

[英]How to create expression input style format from binary table/matrix?

I have a binary table like this in my R script:我的 R 脚本中有一个像这样的二进制表:

>class(forCount)
[1] "table"

>forCount

                          Gene
Filename    CTX-M-27    IMI-1   IMP-39  IMP-4   KPC-2   NDM-1
batch0_01032019_ENT1    0   1   0   0   0   1
batch0_01032019_ENT2    0   0   0   0   1   1
batch0_01032019_ENT3    0   0   0   0   0   1
batch0_01032019_ENT4    0   0   0   0   0   1
batch0_01032019_ENT5    0   0   0   0   0   1
batch0_01032019_ENT6    0   0   0   0   0   1
batch0_01032019_ENT7    0   0   0   0   0   1

How do I get the following information from this?我如何从中获得以下信息?

NDM-1                  5
NDM-1&IMI-1        1
NDM-1&KPC-2      1

Edit1: Above data was dummy data. Edit1:以上数据是虚拟数据。 As per @RonakShah request adding dput information.根据@RonakShah 请求添加 dput 信息。 This is the sample of my data in the table.这是我在表格中的数据样本。

> dput(forCount)
structure(c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L), .Dim = c(6L, 16L), .Dimnames = structure(list(AssemblyFile = c("batch0_01032019_ENT1110", 
"batch0_01032019_ENT1125", "batch0_01032019_ENT1332", "batch0_01032019_ENT1349", 
"batch0_01032019_ENT1449", "batch0_01032019_ENT1607"), CPGene = c("", 
"CTX-M-27", "IMI-1", "IMP-39", "IMP-4", "KPC-2", "NDM-1", "NDM-4", 
"NDM-5", "NDM-7", "NDM-9", "OXA-181", "OXA-23", "OXA-232", "OXA-48", 
"VIM-4")), .Names = c("AssemblyFile", "CPGene")), class = "table")

From the above pasted dput data, I expect the following output, which is out of 6 samples, 5 samples have KPC-2 and 1 sample has both KPC-2 & CTX-M-27.从上面粘贴的 dput 数据中,我预计以下 output,在 6 个样本中,5 个样本具有 KPC-2,1 个样本同时具有 KPC-2 和 CTX-M-27。

KPC-2                       5
KPC-2&CTX-M-27     1

You could convert the table to dataframe and paste the column names in each row which has 1 as value in them and count their occurrence using table .您可以将表格转换为 dataframe 并将列名粘贴到其中值为 1 的每一行中,并使用table计算它们的出现次数。

df <- as.data.frame.matrix(forCount)
table(apply(df, 1, function(x) paste(names(df)[which(x == 1)], collapse = " & ")))

#CTX-M-27 & KPC-2            KPC-2 
#               1                5 

We can convert the data to tibble and then use tidyverse approaches我们可以将数据转换为tibble ,然后使用tidyverse方法

library(dplyr)
as_tibble(forCount) %>%
    filter(n ==1) %>%
    group_by(AssemblyFile) %>% 
    summarise(CPGene = toString(CPGene)) %>%
    count(CPGene)
# A tibble: 2 x 2
#  CPGene              n
#* <chr>           <int>
#1 CTX-M-27, KPC-2     1
#2 KPC-2               5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM