checking for equality

旧城冷巷雨未停 提交于 2019-12-02 13:22:40

Here is an answer which uses the plyrpackage :

library(plyr)
ddply(df, .(Equips), function(d) {
  nb.comps <- length(unique(d$rank))
  tab <- table(d$rank, d$Comps) > 0
  tab <- margin.table(tab, 2)
  return(sum(tab>=nb.comps)>0)
})

Which gives :

  Equips    V1
1      1  TRUE
2      2 FALSE
3      3 FALSE
4      4 FALSE
5      5  TRUE

If you really don't want to use plyr, you can use the by function :

by(df, df$Equips, function(d) {
  nb.comps <- length(unique(d$rank))
  tab <- table(d$rank, d$Comps) > 0
  tab <- margin.table(tab, 2)
  return(sum(tab>=nb.comps)>0)
})

df$Equips: 1
[1] TRUE
-------------------------------------------------------- 
df$Equips: 2
[1] FALSE
-------------------------------------------------------- 
df$Equips: 3
[1] FALSE
-------------------------------------------------------- 
df$Equips: 4
[1] FALSE
-------------------------------------------------------- 
df$Equips: 5
[1] TRUE

If you want to summarize the result you can do something like this :

result <- by(df, df$Equips, function(d) {
  nb.comps <- length(unique(d$Comps))
  tab <- table(d$rank, d$Comps) > 0
  tab <- margin.table(tab, 2)
  return(sum(tab>=nb.comps)>0)
})


data.frame(nb.equips=dim(result), nb.matched=sum(result))

Which gives :

  nb.equips nb.matched
1         5          2
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!