R - 我如何检查对应于给定组的列是否在组内相等?

时间:2016-04-11 17:56:56

标签: r

我们说我有这样的数据:

---
ip: "192.168.10.10"
memory: 2048
cpus: 1
hostname: laraveltest
name: laraveltest
provider: virtualbox

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

folders:
    - map: "/Users/jorge/Dev/laraveltest"
      to: "/home/vagrant/laraveltest"

sites:
    - map: homestead.app
      to: "/home/vagrant/laraveltest/public"

databases:
    - homestead

# blackfire:
#     - id: foo
#       token: bar
#       client-id: foo
#       client-token: bar

# ports:
#     - send: 50000
#       to: 5000
#     - send: 7777
#       to: 777
#       protocol: udp

我如何遍历" group"的所有值?查看与该组对应的值是否具有所有相等的值。我希望有一个数据集,其中只包含值不相同的组。我不确定一个简单的方法来避免for循环。

2 个答案:

答案 0 :(得分:6)

你可以这样做:

tapply(DF$value, DF$group, FUN = function(x) length(unique(x))) > 1L

#     1     2     3     4 
# FALSE  TRUE  TRUE FALSE 

要对表进行子集化,请使用ave

编写相同的表格
DF[ ave(DF$value, DF$group, FUN = function(x) length(unique(x)))  > 1L, ]

#   group value
# 4     2     1
# 5     2     0
# 6     3     1
# 7     3     0

对于包,后一步看起来像......

library(data.table)
setDT(DF)[, if (uniqueN(value) > 1L) .SD, by=group]

# or
library(dplyr)
DF %>% group_by(group) %>% filter(n_distinct(value) > 1L)

答案 1 :(得分:0)

以下是使用table

的其他选项
tbl <- rowSums(table(df1)>0)>1
subset(df1, group %in% names(tbl)[tbl])
#  group value
#4     2     1
#5     2     0
#6     3     1
#7     3     0