从R中的其他列中删除基于criteron的行

从R中的其他列中删除基于criteron的行,r,R,我正在尝试筛选R中的一些数据。其格式如下: id config_id alpha begin end day 1 1 1 5 138 139 6 2 1 2 5 137 138 6 3 1 3 5 47

我正在尝试筛选R中的一些数据。其格式如下:

          id config_id alpha         begin         end day
1          1         1     5           138         139   6
2          1         2     5           137         138   6
3          1         3     5            47          48   2
4          1         3     3            46          47   2
5          1         4     3            45          46   2
6          1         4     3            43          44   2

...

          id config_id alpha         begin         end day
1          2         1     5           138         139   6
2          2         2     5           137         138   6
3          2         2     5           136         137   6
4          2         3     3            45          46   2
5          2         3     3            44          45   2
6          2         4     3            43          44   2
我的目标是删除任何导致在同一天开始和结束的配置。例如,在上面的示例中,
config\u id
3是不可接受的,因为
config\u id
的两个实例都发生在
2。
config\u id
4的情况也一样。在下面的示例中,
config_id
2和
config_id
3出于同样的原因是不可接受的

基本上,如果我有一个重复的
config\u id
,并且任何
day
(来自
day
)列都会多次显示该
config\u id
,那么我想从列表中删除该
config\u id

现在我正在使用一种相当复杂的
lappy
算法,但肯定有一种更简单的方法


谢谢

假设您的数据存储在一个名为
my_data
的数据框中,您可以通过几种方式执行此操作

基尔
相同(第1天)
姓名(当天)[2]%
过滤器(!当天)
数据表
库(data.table)

my_data我们还可以使用
n_distinct
from
dplyr
。在这里,我按“id”和“config_id”进行分组,然后使用
过滤器删除行。如果组中的元素数大于1(
n()>1)
,(
&
),则“天”中的不同元素数等于1(
n_distinct==1
),我们将其删除

library(dplyr)
df1 %>% 
   group_by(id, config_id) %>% 
   filter(!(n()>1 & n_distinct(day)==1))
#Source: local data frame [4 x 6]
#Groups: id, config_id [4]

#     id config_id alpha begin   end   day
#   (int)     (int) (int) (int) (int) (int)
#1     1         1     5   138   139     6
#2     1         2     5   137   138     6
#3     2         1     5   138   139     6
#4     2         4     3    43    44     2
如果我们对同一个“配置id”有不同的“日期”,这也应该起作用

df1$day[4] <- 3
数据
df1发布您的代码,这样我们就可以看到您想要改进的地方。实际上,我并没有让它完全工作,我希望从零开始得到一个更干净的解决方案。。。
library(data.table)
my_data <- data.table(my_data, key = "config_id")
same_day <- my_data[, .(same_day = any(table(day) > 1)), by = "config_id"]
my_data[!my_data[same_day]$same_day, ]
library(dplyr)
df1 %>% 
   group_by(id, config_id) %>% 
   filter(!(n()>1 & n_distinct(day)==1))
#Source: local data frame [4 x 6]
#Groups: id, config_id [4]

#     id config_id alpha begin   end   day
#   (int)     (int) (int) (int) (int) (int)
#1     1         1     5   138   139     6
#2     1         2     5   137   138     6
#3     2         1     5   138   139     6
#4     2         4     3    43    44     2
df1$day[4] <- 3
library(data.table)#v1.9.6+
setDT(df1)[, if(!(.N>1 & uniqueN(day) == 1L)) .SD, by = .(id, config_id)]
df1 <- structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L), config_id = c(1L, 2L, 3L, 3L, 4L, 4L, 1L, 2L, 2L, 3L, 
3L, 4L), alpha = c(5L, 5L, 5L, 3L, 3L, 3L, 5L, 5L, 5L, 3L, 3L, 
3L), begin = c(138L, 137L, 47L, 46L, 45L, 43L, 138L, 137L, 136L, 
45L, 44L, 43L), end = c(139L, 138L, 48L, 47L, 46L, 44L, 139L, 
138L, 137L, 46L, 45L, 44L), day = c(6L, 6L, 2L, 2L, 2L, 2L, 6L, 
6L, 6L, 2L, 2L, 2L)), .Names = c("id", "config_id", "alpha", 
"begin", "end", "day"), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"))