R-将重复项提取到数据框

发布于 2025-01-25 10:08:02 字数 4653 浏览 2 评论 0原文

我需要r的帮助,类似于问题 filtering-a-a-a-dataframe-showing-showning-show-show-show-show-show-sonly-deplicates 我希望从具有超过2,000个条目的数据框架中提取重复。

前15行数据看起来像这样:

运行ID差异
1200
141024
141
141 4
4465
11 41
11 41
111475
111 111 1
111 1 1 111
2250
218 0 2 180
2181
2181
2181

我只想提取重复项,即

运行IDdiff
141024
141
141 4
1465
14 1 41
411
111475
1111
11 475 1 11 1 1 1111
2180
2181
2181
2181

使用命令

mydata_extract%>%group_by(id)%>%filter(n(n()> 1) 不提取数据,实际上我会返回完整的数据集。我需要更改的“滤镜(n()> 1)”有什么东西吗?我是R的初学者。 抱歉,我的数据表未正确格式化,预览看起来还可以!

我还将首先通过“运行”对数据进行分组

I need help with R, similar to question filtering-a-dataframe-showing-only-duplicates I wish to extract duplicates from a dataframe with over 2,000 entries.

The first 15 rows of data looks like this:

runidDiff
1200
141024
141
141
1465
141
141
111475
1111
1111
2250
2180
2181
2181
2181

I wish to extract only the duplicates, i.e.

runidDiff
141024
141
141
1465
141
141
111475
1111
1111
2180
2181
2181
2181

Using the command

mydata_extract %>% group_by(id) %>% filter(n() > 1)
does not extract the data, in fact I get the complete set of data returned. Is there something about "filter(n() > 1)" that I need to change? I'm a beginner with R.
Sorry my data table is not formatting correctly, it looks okay in preview!

I will also want to group my data first by "run"

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

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

发布评论

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

评论(1

罗罗贝儿 2025-02-01 10:08:02

也许在group_by()中添加运行和ID?

  library(dplyr)
   df <- tibble::tribble(
      ~"run", ~"id", ~"Diff",
      1, 20, 0,
      1, 4, 1024,
      1, 4, 1,
      1, 4, 1,
      1, 4, 65,
      1, 4, 1,
      1, 4, 1,
      1, 11, 4,
      1, 11, 1,
      1, 11, 1,
      2, 25, 0,
      2, 18, 0,
      2, 18, 1,
      2, 18, 1,
      2, 18, 1
    ) %>% 
     group_by(run, id) %>% 
      filter(n()>1)



   # A tibble: 13 x 3
# Groups:   run, id [3]
     run    id  Diff
   <dbl> <dbl> <dbl>
 1     1     4  1024
 2     1     4     1
 3     1     4     1
 4     1     4    65
 5     1     4     1
 6     1     4     1
 7     1    11     4
 8     1    11     1
 9     1    11     1
10     2    18     0
11     2    18     1
12     2    18     1
13     2    18     1

您可以添加一个突变,以查看此n()的工作方式(计数每个组的行数),例如

df %>% 
 group_by(run, id) %>% 
  mutate(n = n()) 

Maybe add run and id in the group_by()?

  library(dplyr)
   df <- tibble::tribble(
      ~"run", ~"id", ~"Diff",
      1, 20, 0,
      1, 4, 1024,
      1, 4, 1,
      1, 4, 1,
      1, 4, 65,
      1, 4, 1,
      1, 4, 1,
      1, 11, 4,
      1, 11, 1,
      1, 11, 1,
      2, 25, 0,
      2, 18, 0,
      2, 18, 1,
      2, 18, 1,
      2, 18, 1
    ) %>% 
     group_by(run, id) %>% 
      filter(n()>1)



   # A tibble: 13 x 3
# Groups:   run, id [3]
     run    id  Diff
   <dbl> <dbl> <dbl>
 1     1     4  1024
 2     1     4     1
 3     1     4     1
 4     1     4    65
 5     1     4     1
 6     1     4     1
 7     1    11     4
 8     1    11     1
 9     1    11     1
10     2    18     0
11     2    18     1
12     2    18     1
13     2    18     1

You can add a mutate, to see how this n() works (counts the number of rows per group),e.g.

df %>% 
 group_by(run, id) %>% 
  mutate(n = n()) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文