带R的条件格式表

时间:2014-11-16 02:59:04

标签: r duplicates aggregate apply

我有一张桌子:

Time Class  Type Value1 Value2
2005  A      KS    5      6
2005  B      KS    3      3
2005  C      CS    6      6
2006  A      CS    5      3
2006  A      KS    9      2
2006  B      KS    6      9
2006  C      KS   39      6
2007  C      CS   10      20
2007  A      KS   26      23

我需要创建新的数据集,其中每年只包含具有相同“类”和“类型”的行。例如,2005年和2006年只有两行具有相同(通用)“类”和“类型”。

1 个答案:

答案 0 :(得分:1)

尝试

indx1 <- Reduce(`intersect`,lapply(split(df, df$Time),
      function(x) as.character(interaction(x[,c('Class', 'Type')]))))

df[as.character(interaction(df[,c('Class', 'Type')])) %in% indx1,]
#  Time Class Type Value1 Value2
#1 2005     A   KS      5      6
#5 2006     A   KS      9      2
#9 2007     A   KS     26     23

数据

  df <- structure(list(Time = c(2005L, 2005L, 2005L, 2006L, 2006L, 2006L, 
  2006L, 2007L, 2007L), Class = c("A", "B", "C", "A", "A", "B", 
  "C", "C", "A"), Type = c("KS", "KS", "CS", "CS", "KS", "KS", 
  "KS", "CS", "KS"), Value1 = c(5L, 3L, 6L, 5L, 9L, 6L, 39L, 10L, 
  26L), Value2 = c(6L, 3L, 6L, 3L, 2L, 9L, 6L, 20L, 23L)), .Names = c("Time", 
  "Class", "Type", "Value1", "Value2"), class = "data.frame", row.names =
  c(NA, -9L))