寻找最小设定长度

时间:2014-04-09 08:30:44

标签: r set data.table

我每月和每年都有电表读数:

df = read.table(header = T, text = "
year    month   meter
2012    1   A
2012    2   A
2013    1   A
2013    2   A
2012    1   B
2012    2   B
2013    1   B
2013    2   B
2012    1   D
2012    2   D ")

问题:如何获取在整个计量期间[最小设定长度,见下文]中有效的电表组?

E.g。对于此样本数据集,结果将是:

2012 / 1 = {A,B,D}
2012 / 2 = {A,B,D}
2013 / 1 = {A,B}
2013 / 2 = {A,B}
# minimum set length = {A,B}

# I have made the datasets in data.table but get stuck at the final bit;
# probably a set operation over column V1 below.
require(data.table)    
df = data.table(df)
(df.sets = df[,paste0(unique(meter),collapse = ','), keyby = list(year,month)])

1 个答案:

答案 0 :(得分:0)

你知道这个功能吗?分裂

names(which(sapply(split.data.frame(df,f=df$meter),nrow)==nrow(unique(df[,c("year","month")]))))