【问题标题】:How do I keep and separate the data that is unique to each method, in all three methods, or in just two methods?如何保留和分离每种方法、所有三种方法或仅两种方法中唯一的数据?
【发布时间】:2019-11-04 08:51:10
【问题描述】:

我有一个数据集,其中我从每种不同的实验方法(梯度、等度和 HILIC)中识别出化合物。我希望能够存储/子集结果,以便我获得在所有三种方法中识别的化合物,每种方法独有,以及两种方法之间的重叠。最终,我将能够在三种方法和已识别的化合物之间创建一个维恩图类型的图形。

我在“Compound”列上尝试了“unique()”和“subset()”,但我不知道如何为该方法添加另一个条件。

'''head(Data, n = 12)'''
       Precursor.Ion         Compound    Method
1            141             Methanol  Gradient
2            143             Methanol  Gradient
3             82              Toluene  Gradient
4             54          Isopropanol  Gradient
5             47                Water  Gradient
6             45                Water  Gradient
7            135              Toluene Isocratic
8             82          Acetonirile Isocratic
9             91              Acetone Isocratic
10            43              Toluene     HILIC
11            31          Isopropanol     HILIC
12            97 Methyltertbutylether     HILIC

'''unique(Data$Compound)'''
Methanol             Toluene              Isopropanol          Water               
Acetonirile          Acetone              Methyltertbutylether

我希望输出是这样的,存储在以下变量中

'''All'''
Toluene
'''Gradient'''
Methanol   Water
'''Isocratic'''
Acetone, Acetonitrile
'''HILIC'''
Methyltertbutylether
'''Gradient and Isocratic'''
N/A
'''Gradient and HILIC'''
Isopropanol
'''Isocratic and HILIC'''
N/A

【问题讨论】:

    标签: r duplicates subset unique


    【解决方案1】:

    这是一个相对简单的 Tidyverse 方法,遗憾的是错过了 N/A 条目。如果您真的需要这些,可以在之后添加它们。 (另外Gradient, Isocratic, HILIC对应All。)

    > library(dplyr)
    > library(readr)
    > tbl <- read_table("
    +       141             Methanol  Gradient
    +       143             Methanol  Gradient
    +        82              Toluene  Gradient
    +        54          Isopropanol  Gradient
    +        47                Water  Gradient
    +        45                Water  Gradient
    +       135              Toluene Isocratic
    +        82          Acetonirile Isocratic
    +        91              Acetone Isocratic
    +        43              Toluene     HILIC
    +        31          Isopropanol     HILIC
    +        97 Methyltertbutylether     HILIC
    + ",
    + col_names=c("Precursor.Ion", "Compound", "Method"),
    + col_types=cols(Precursor.Ion=col_integer(), Compound=col_character(), Method=col_character()))
    > collapse <- ", "
    > tbl %>%
    +     group_by(Compound) %>%
    +     summarize(Methods=sort(paste(unique(Method), collapse=collapse))) %>%
    +     group_by(Methods) %>%
    +     summarize(Compounds=sort(paste(unique(Compound), collapse=collapse)))
    # A tibble: 5 x 2
      Methods                    Compounds           
      <chr>                      <chr>               
    1 Gradient                   Methanol, Water     
    2 Gradient, HILIC            Isopropanol         
    3 Gradient, Isocratic, HILIC Toluene             
    4 HILIC                      Methyltertbutylether
    5 Isocratic                  Acetone, Acetonirile
    

    【讨论】:

    • 嗨 d125q。谢谢您的帮助。 N/A 不出现也没关系!有没有办法为它提供数据框而不是 tbl?我需要使用 grouped_df 而不是 group_by 吗?
    • 您可以使用数据框代替小标题,没有任何问题。
    • 我不断收到此错误,“UseMethod("group_by_") 中的错误:没有适用于 'group_by_' 的方法应用于“因子”类的对象”
    • 我已经将每一列重新分类为类型字符,就像你上面所说的那样。代码如下:Data %>% + group_by(Compound) %>% + summarise(Method=sort(paste(unique(Method), collapse=collapse))) %>% + grouped_by(Method) %>% +总结(复合=排序(粘贴(唯一(复合),折叠=折叠)))
    【解决方案2】:

    使用我的nVennR 包将非常容易。随着表存储在myT

    > library(nVennR)
    > grad <- subset(myT, Method == "Gradient")$Compound
    > iso <- subset(myT, Method == "Isocratic")$Compound
    > hil <- subset(myT, Method == "HILIC")$Compound
    > myV <- plotVenn(list(Gradient=grad, Isocratic=iso, HILIC=hil))
    
    > listVennRegions(myV)
    $`0, 0, 1 (HILIC)`
    [1] "Methyltertbutylether"
    
    $`0, 1, 0 (Isocratic)`
    [1] "Acetonirile" "Acetone"    
    
    $`1, 0, 0 (Gradient)`
    [1] "Methanol" "Water"   
    
    $`1, 0, 1 (Gradient, HILIC)`
    [1] "Isopropanol"
    
    $`1, 1, 1 (Gradient, Isocratic, HILIC)`
    [1] "Toluene"
    
    
    > listVennRegions(myV, na.rm = F)
    $`0, 0, 0 ()`
    [1] NA
    
    $`0, 0, 1 (HILIC)`
    [1] "Methyltertbutylether"
    
    $`0, 1, 0 (Isocratic)`
    [1] "Acetonirile" "Acetone"    
    
    $`0, 1, 1 (Isocratic, HILIC)`
    [1] NA
    
    $`1, 0, 0 (Gradient)`
    [1] "Methanol" "Water"   
    
    $`1, 0, 1 (Gradient, HILIC)`
    [1] "Isopropanol"
    
    $`1, 1, 0 (Gradient, Isocratic)`
    [1] NA
    
    $`1, 1, 1 (Gradient, Isocratic, HILIC)`
    [1] "Toluene"
    

    你还会得到一个维恩图:

    【讨论】:

    • 这是完美的 vqf。我的维恩图有一个错误。你以前见过这个错误吗?警告消息:在 showSVG(myVenn, ...) 中:图形无法在绘图窗口中呈现。请使用参数 outFile 和/或 systemShow。
    • 是的,这意味着您的系统没有公认的方式来绘制维恩图。为此,您需要 rsvggrImport2 包,以及绘图窗口(例如,您不能直接在命令行环境中绘图)。命令提示可以直接写myV &lt;- plotVenn(list(Gradient=grad, Isocratic=iso, HILIC=hil), outFile=path_to_exported_picture)导出svg图。
    猜你喜欢
    • 1970-01-01
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 2020-11-06
    相关资源
    最近更新 更多