【问题标题】:subsetting columns based on columns containing duplicates基于包含重复项的列子集列
【发布时间】:2013-01-11 13:20:05
【问题描述】:

我想根据具有重复 ID 的列之一对 3 列进行子集化,这样我只能得到 3 个具有唯一值的列

structure(list(ID = 1:4, x = c(46L, 47L, 47L, 47L), y = c(5L, 

6L, 7L, 7L)), .Names = c("ID", "x", "y"), row.names = c(1L, 6L, 11L, 16L), class= "data.frame")

【问题讨论】:

  • 我想返回 3 列,但是当它们不重复时。
  • 不是这样,你的公式确实有效。我想返回三列的数据框,但没有重复。

标签: r unique


【解决方案1】:

在数据框上使用重复的方法应该可以:

dat[!duplicated(dat),] # (equivalent here to dat[!duplicated(dat$ID),] )
   ID  x y
1   1 46 5
6   2 47 6
11  3 47 7
16  4 47 7

【讨论】:

  • > new_data=data[!duplicated(data$ID),] > new_data 这里是输出: structure(list(ID = 1:4, x = c(46L, 47L, 47L, 47L ), y = c(5L, 6L, 7L, 7L)), .Names = c("ID", "x", "y"), row.names = c(1L, 6L, 11L, 16L), class= "data.frame")
  • 感谢大家的回复
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-30
  • 1970-01-01
  • 1970-01-01
  • 2015-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多