Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何比较R中一个数据帧的行?_R_Dataframe_Compare_Rows_Bioinformatics - Fatal编程技术网

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

如何比较R中一个数据帧的行?,r,dataframe,compare,rows,bioinformatics,R,Dataframe,Compare,Rows,Bioinformatics,我有一个包含很多行和至少13列的数据框架。我需要将每一行与前一行进行比较,看看这两列中的行是否完全相同,而其余的行是否不同 如果两列中的两行相等,我想将这些行放在一个新的数据框中 这是我的数据框 前三行有样本“sample”,但只有两行是相同的“Gene”。第7行和第8行的样本和基因也相同 我想要一个新的数据框,只包含具有相同样本和相同基因的行。像这样: 我写了这段代码: Vec_sample <- c() Vec_genes <- c() Vec_variants &l

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

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

这是我的数据框

前三行有样本“sample”,但只有两行是相同的“Gene”。第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])
      }
    }
  }
}

如果我理解正确,您可以使用
dplyr:filter
,使用
lead
lag
检查上一行和下一行

df%过滤器(样本==滞后(样本)|样本==领先(样本),
基因==滞后(基因)|基因==领先(基因))
#>样本基因变异Chr坐标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

由(v0.3.0)于2020-08-11创建,这绝对不是我们在R中所做的。要比较行,您可以从如下内容开始:
head(DF,-1)==tail(DF,-1)
。您不需要循环,您将使用子集提取行。请不要以图片形式提供数据。在答案中以(格式化)文本的形式提供。我们可以从图片中复制数据。请您将数据作为文本而不是图像提供。最好使用
dput(dataframe)
以便于导入为什么不包括第1行和第2行?他们有相同的样本和基因,不是吗?@Roland很抱歉。我不能附加图片,只有链接,因为我是新来的。这很有效!但在我的例子中,我没有想到基因可能不在一起。。。(我想用一个简化的例子来解释它,但我忘记了这一点)。我再次输入了数据,很抱歉出错。关于我的另一条消息,我解决了这个问题,按基因排序,现在没有问题了。非常感谢你!!!
final_dataframe <- data.frame(Vec_sample, Vec_genes, Vec_variants, Vec_chr, Vec_coordinate, Vec_aa, Vec_Rs).
 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")