如何比较 R 中 ONE 数据帧的行?

How can I compare rows of ONE dataframe in R?

我有一个包含很多行和至少 13 列的数据框。我需要将每一行与前一行进行比较,以查看它是否在两列中完全相同而在其余列中是否不同。

如果两行在两列中相等,我想将这些行放在新的数据框中。

这是我的数据框。

前三行是样本“样本”,但只有两行是相同的“基因”。第 7 行和第 8 行也有相同的样本和基因。

我想要一个新的数据框,其中只有具有相同样本和相同基因的行。像这样:

我写了这段代码:

Vec_sample <- c()    
Vec_genes <- c()
Vec_variants <- c()
Vec_chr <- c()
Vec_coordinate <- c()
Vec_aa <- c()
Vec_Rs <- c()
`%notin%` <- Negate(`%in%`)


for (row in 1:nrow(dataframe))
{
  for (row_compare in 1:nrow(dataframe))
  {
    if ((dataframe$Gene[row] == dataframe$Gene[row_compare]) 
        & (row != row_compare))
    {
      if ((dataframe$Sample[row] %notin% Vec_sample) &
          (dataframe$Sample[row] == dataframe$Sample[row_compare]))
      {
        
        Vec_sample <- c(Vec_sample , dataframe$Sample[row])
        Vec_sample <- c(Vec_sample , dataframe$Sample[row_compare])
        Vec_genes <- c(Vec_genes, dataframe$Gene[row])
        Vec_genes <- c(Vec_genes, dataframe$Gene[row_compare])
        Vec_variants <- c(Vec_variants , dataframe$Variants[row])
        Vec_variants <- c(Vec_variants , dataframe$Variants[row_compare])
        Vec_chr <- c(Vec_chr , dataframe$Chr[row])
        Vec_chr <- c(Vec_chr , dataframe$Chr[row_compare])
        Vec_coordinate <- c(Vec_coordinate, dataframe$Coordinate[row])
        Vec_coordinate <- c(Vec_coordinate, dataframe$Coordinate[row_compare])
        Vec_aa <- c(Vec_aa , dataframe$aa[row])
        Vec_aa <- c(Vec_aa , dataframe$aa[row_compare])
        Vec_Rs <- c(Vec_Rs , dataframe$Rs[row])
        Vec_Rs <- c(Vec_Rs , dataframe$Rs[row_compare])
      }
    }
  }
}

最后,当循环结束时,我用结果创建了一个数据框。

final_dataframe <- data.frame(Vec_sample, Vec_genes, Vec_variants, Vec_chr, Vec_coordinate, Vec_aa, Vec_Rs).

一切都在循环中复制,因为我需要一对相等的样本和基因(当然,还有其余信息)。 我写了两个 for 循环,因为我想将实际基因与另一个进行比较。

有问题吗?如果样本已经保存在向量“Vec_sample”中,如果有另一对样本具有相同的样本,我的脚本将不会保存这对样本。 (比如样本14-043,首先会保存ALG9基因对,但不会保存MNS1基因对)

这是我的错误的新数据框

我放那个例外是因为当我 运行 这两个循环时, table 会被检查不止一次,它会多次保存基因对并且会重复。

抱歉,如果我的语法或编程方式效率低下,我是这个世界的新手,并不是真正的专家。

我希望我已经解释清楚了。 非常感谢您

我提供输入数据。

 structure(list(Sample = c("14-043", "14-043", "14-043", "14-043", 
"14-043", "14-043", "14-077", "14-077", "13-340", "15-642", "15-642", 
"15-642", "12-975"), Gene = c("ALG9", "ALG10B", "ALG9", "SLC5A9", 
"MNS1", "MNS1", "ALG9", "ALG9", "GPI", "MNS1", "HK3", "MNS1", 
"HK3"), Variant = c("T>T/G", "C>A/G", "C>C/G", "A>A/T", "A>T/T", 
"C>C/T", "T>T/G", "C>C/G", "C>G/G", "A>T/T", "T>T/A", "C>C/T", 
"T>T/A"), Chr = c(4, 4, 4, 13, 2, 2, 4, 4, 20, 2, 8, 2, 8), Coordinate = c(23410158, 
3422351, 23410451, 2341043423, 324652341, 3246520, 23410158, 
23410451, 234541, 324652341, 23412341, 3246520, 23412341), aa = c("Gly44Thr", 
"His8Pro", "Ser44Thr", "Thr4Pro", "Ala45Ala", "Ala45Leu", "Gly44Thr", 
"Ser44Thr", "Phe3Ala", "Ala45Ala", "Val34His", "Ala45Leu", "Val34His"
), Rs = c("rs1715919", "rs1734532413", "rs1732413", "rs173240", 
"rs12305", "rs10356", "rs1715919", "rs1732413", "rs12342", "rs12305", 
"rs9997", "rs10356", "rs9997")), row.names = c(NA, -13L), class = "data.frame")

如果我没理解错的话,你可以使用 dplyr:filter,使用 leadlag 来检查前后行

df <- structure(list(Sample = c("14-043", "14-043", "14-043", "14-043", 
                          "14-043", "14-077", "14-077", "13-340", "15-642", "15-642", "12-975"
), Gene = c("ALG9", "ALG9", "SLC5A9", "MNS1", "MNS1", "ALG9", 
            "ALG9", "GPI", "MNS1", "MNS1", "HK3"), Variant = c("T>T/G", "C>C/G", 
                                                               "A>A/T", "A>T/T", "C>C/T", "T>T/G", "C>C/G", "C>G/G", "A>T/T", 
                                                               "C>C/T", "T>T/A"), Chr = c(4, 4, 13, 2, 2, 4, 4, 20, 2, 2, 8), 
Coordinate = c(23410158, 23410451, 2341043423, 324652341, 
               3246520, 23410158, 23410451, 234541, 324652341, 3246520, 
               23412341), aa = c("Gly44Thr", "Ser44Thr", "Thr4Pro", "Ala45Ala", 
                                 "Ala45Leu", "Gly44Thr", "Ser44Thr", "Phe3Ala", "Ala45Ala", 
                                 "Ala45Leu", "Val34His"), Rs = c("rs1715919", "rs1732413", 
                                                                 "rs173240", "rs12305", "rs10356", "rs1715919", "rs1732413", 
                                                                 "rs12342", "rs12305", "rs10356", "rs9997")), row.names = c(NA, 
                                                                                                                            -11L), class = "data.frame")
library(tidyverse)
df %>% filter(Sample == lag(Sample) | Sample == lead(Sample), 
              Gene == lag(Gene) | Gene == lead(Gene))
#>   Sample Gene Variant Chr Coordinate       aa        Rs
#> 1 14-043 ALG9   T>T/G   4   23410158 Gly44Thr rs1715919
#> 2 14-043 ALG9   C>C/G   4   23410451 Ser44Thr rs1732413
#> 3 14-043 MNS1   A>T/T   2  324652341 Ala45Ala   rs12305
#> 4 14-043 MNS1   C>C/T   2    3246520 Ala45Leu   rs10356
#> 5 14-077 ALG9   T>T/G   4   23410158 Gly44Thr rs1715919
#> 6 14-077 ALG9   C>C/G   4   23410451 Ser44Thr rs1732413
#> 7 15-642 MNS1   A>T/T   2  324652341 Ala45Ala   rs12305
#> 8 15-642 MNS1   C>C/T   2    3246520 Ala45Leu   rs10356

reprex package (v0.3.0)

于 2020-08-11 创建