从数据框中提取重复行

发布于 2024-12-10 20:00:13 字数 800 浏览 0 评论 0原文

我正在处理一个大型数据框,前几行如下:

      Assay   Genotype   Sample    Result
1     001        G         1         0
2     001        A         2         1
3     001        G         3         0 
4     001        NA        1         NA
5     002        T         1         0
6     002        G         2         1
7     002        T         2         0 
8     002        T         4         0
9     003        NA        1         NA

总共我将处理 2000 个样本,每个样本进行 168 次检测。

我想提取具有相同测定和样品的多个条目的行。我希望结果数据位于包含所有重复条目的数据框中,并进行排序以使重复项彼此相邻。从上面的示例来看,结果将如下所示:

      Assay   Genotype   Sample    Result
1     001        G         1         0
4     001        NA        1         NA
6     002        G         2         1
7     002        T         2         0 

I have a large data frame that Im working with, the first few lines are as follows:

      Assay   Genotype   Sample    Result
1     001        G         1         0
2     001        A         2         1
3     001        G         3         0 
4     001        NA        1         NA
5     002        T         1         0
6     002        G         2         1
7     002        T         2         0 
8     002        T         4         0
9     003        NA        1         NA

In total I'll be working with 2000 samples and 168 Assays for each sample.

Id like to extract the lines where I have multiple entries with both the same Assay and Sample. I want the resulting data to be in a data frame containing all of the duplicate entries, sorted such that the duplicates are next to each other. From the example above the result would look like this:

      Assay   Genotype   Sample    Result
1     001        G         1         0
4     001        NA        1         NA
6     002        G         2         1
7     002        T         2         0 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日久见人心 2024-12-17 20:00:13

易于加载的演示数据:

df <- structure(list(Assay = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L), Genotype = structure(c(2L, 1L, 2L, NA, 3L, 2L, 3L, 3L, NA), .Label = c("A", "G", "T"), class = "factor"), Sample = c(1L, 2L, 3L, 1L, 1L, 2L, 2L, 4L, 1L), Result = c(0L, 1L, 0L, NA, 0L, 1L, 0L, 0L, NA)), .Names = c("Assay", "Genotype", "Sample", "Result"), class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9"))

您可以使用duplicated轻松获得重复的检测/样品对:

vars <- c('Assay', 'Sample')
dup <- df[duplicated(x[, vars]), vars]

结果:

> dup
  Assay Sample
4     1      1
7     2      2

需要一个简单的合并来获得所需的结果:

> merge(dup, df)
  Assay Sample Genotype Result
1     1      1     <NA>     NA
2     1      1        G      0
3     2      2        G      1
4     2      2        T      0

Demo data for easy loading:

df <- structure(list(Assay = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L), Genotype = structure(c(2L, 1L, 2L, NA, 3L, 2L, 3L, 3L, NA), .Label = c("A", "G", "T"), class = "factor"), Sample = c(1L, 2L, 3L, 1L, 1L, 2L, 2L, 4L, 1L), Result = c(0L, 1L, 0L, NA, 0L, 1L, 0L, 0L, NA)), .Names = c("Assay", "Genotype", "Sample", "Result"), class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9"))

You could easily get the dupicated Assay/Sample pairs with duplicated:

vars <- c('Assay', 'Sample')
dup <- df[duplicated(x[, vars]), vars]

Resulting in:

> dup
  Assay Sample
4     1      1
7     2      2

Which needs a simple merge for required result:

> merge(dup, df)
  Assay Sample Genotype Result
1     1      1     <NA>     NA
2     1      1        G      0
3     2      2        G      1
4     2      2        T      0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文