【发布时间】:2015-02-16 16:00:15
【问题描述】:
想象一下我有一个这样的数据框:
> col1 <- rep(1:3,10)
> col2 <- rep(c("a","b"),15)
> col3 <- rnorm(30,10,2)
> sample_df <- data.frame(col1 = col1, col2 = col2, col3 = col3)
> head(sample_df)
col1 col2 col3
1 1 a 13.460322
2 2 b 3.404398
3 3 a 8.952066
4 1 b 11.148271
5 2 a 9.808366
6 3 b 9.832299
我只想保留 col3 标准差低于 2 的预测变量组合。我可以使用 ddply 找到组合,但我不知道如何回溯到原始 DF 并选择正确的级别。
> sample_df_summ <- ddply(sample_df, .(col1, col2), summarize, sd = sd(col3), count = length(col3))
> head(sample_df_summ)
col1 col2 sd count
1 1 a 2.702328 5
2 1 b 1.032371 5
3 2 a 2.134151 5
4 2 b 3.348726 5
5 3 a 2.444884 5
6 3 b 1.409477 5
为了清楚起见,在此示例中,我想要 col1 = 3、col2 = b 和 col1 = 1 和 col 2 = b 的 DF。我该怎么做?
【问题讨论】: