由两个因子变量子集

时间:2018-06-15 00:40:42

标签: r aggregate factors

我想考虑两个因素(fac1fac2)之间的相互关系来聚合我的数据集,并为此应用函数。例如,考虑

给出的数据集
set.seed(1)
test <- data.frame(fac1 = sample(c("A", "B", "C"), 30, rep = T), 
                   fac2 = sample(c("a", "b"), 30, rep = T), 
                   value = runif(30))

对于fac1 == "A""fac2 == a",我们有五个值,我想按分钟汇总。使用残酷的力量我试过这种方式

min(test[test$fac1 == "A" & test$fac2 == "a", ]$value)

2 个答案:

答案 0 :(得分:2)

你提到this.swPush.subscription.subscribe(sub => { sub.unsubscribe().then(() => { this.dataSvc.deleteOne({kind: 'Notification', id: sub.endpoint}).subscribe(result => { this.toastr.success('Notifications unsubscribed.') }) }).catch(err => this.toastr.error('Failed to unsubscribe to notifications.')) }) ,这将在这里工作。

aggregate

答案 1 :(得分:1)

这是tidyverse替代

test %>% group_by(fac1, fac2) %>% summarise(x = min(value))
## A tibble: 6 x 3
## Groups:   fac1 [?]
#  fac1  fac2       x
#  <fct> <fct>  <dbl>
#1 A     a     0.325
#2 A     b     0.339
#3 B     a     0.143
#4 B     b     0.0842
#5 C     a     0.332
#6 C     b     0.245