【问题标题】:find unique rows depending on ID in R根据 R 中的 ID 查找唯一行
【发布时间】:2015-04-02 08:42:15
【问题描述】:

这只是我整个数据框的一小部分(鱼的年份和学名):

dput(fishery)
structure(list(Year = c(2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 
2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 
2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 
2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 
2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 
2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 
2009L, 2009L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L), Scientific.Name = structure(c(15L, 
1L, 17L, 18L, 1L, 1L, 18L, 1L, 18L, 19L, 1L, 15L, 1L, 18L, 15L, 
17L, 15L, 1L, 18L, 1L, 18L, 1L, 1L, 17L, 18L, 18L, 17L, 1L, 15L, 
18L, 18L, 18L, 1L, 17L, 1L, 1L, 1L, 1L, 17L, 18L, 17L, 18L, 18L, 
22L, 15L, 18L, 18L, 18L, 2L, 1L, 1L, 18L, 18L, 18L, 1L, 18L, 
2L, 17L, 17L, 19L, 7L, 18L, 2L, 18L, 17L, 7L, 18L, 18L, 18L, 
1L, 1L, 17L, 18L, 18L, 1L, 1L, 17L, 19L, 17L, 1L, 1L, 1L, 17L, 
1L, 18L, 18L, 18L, 18L, 18L, 17L, 18L, 13L, 17L, 17L, 18L, 17L, 
7L, 15L, 16L, 1L), .Label = c("Aristaeomorpha foliacea", "Aristeus antennatus", 
"Centrophorus granulosus", "Citharus linguatula", "Dipturus oxyrinchus", 
"Gadus morhua", "Helicolenus dactylopterus", "Heptranchias perlo", 
"Hexanchus griseus", "Lepidopus caudatus", "Lepidorhombus boscii", 
"Leucoraja melitensis", "Lophius budegassa", "Lophius spp", "Merluccius merluccius", 
"Mullus surmuletus", "Nephrops norvegicus", "Parapenaeus longirostris", 
"Phycis blennoides", "Raja clavata", "Raja montagui", "Scyliorhinus canicula", 
"Todarodes sagittatus", "Torpedo nobiliana", "Trachurus spp", 
"Aspitrigla cuculus", "Illex coindetii", "Leucoraja circularis", 
"Sepia elegans"), class = "factor")), class = "data.frame", row.names = c(NA, 
-100L), .Names = c("Year", "Scientific.Name"))

我想知道与每个Year 进行比较的Scientific.Name 字段。

例如,Scientific.Name 列的名称 Aristaeomorpha foliacea 是 2009 年和 2010 年的常见条目,而 Scyliorhinus canicula 仅在 2009 年而非 2010 年被发现。

我希望我说得够清楚..

顺便说一句,整个数据框非常庞大..

谢谢

【问题讨论】:

  • 这有点相似,应该可以帮助你stackoverflow.com/questions/29143857/…
  • library(data.table); setDT(fishery); fishery[ , .N , by = c("Year","Scientific.Name") ]
  • with(fishery, table( Year, Scientific.Name))
  • 你可以试试library(dplyr);fishery %>% group_by(Scientific.Name) %>% filter(n_distinct(Year)==2);

标签: r subset


【解决方案1】:

table(fishery$Scientific.Name , fishery$Year) 这样简单的东西就不能工作吗?

我不太明白上面例子中年份到名字的映射关系。

【讨论】:

  • (是的,如果我有足够的分数,我会发表评论)。
  • 嗨@spitshine..你的答案是好的,但这并不是我想要的。我正在尝试“过滤”Scientific.Name 列,以便仅显示始终捕获的常见鱼类。所以我对仅在 2009 年而不是在 2010 年捕获的物种不感兴趣。无论如何,谢谢!
  • 您需要更具体一点:您要查看连续几年、多年还是所有年份发现的物种?答案很可能是插入一个计数列(使用 data.table 最简单,请参阅上面的@Simon 评论)并对其进行选择。
猜你喜欢
  • 2022-01-07
  • 2013-11-21
  • 2020-03-05
  • 2022-09-23
  • 2017-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多