library(data.table)
library(dplyr)
df = data.table(V1 = c("A", "A", "B", "B"), V2 = c(1, 100, 2, 100))
df
## V1 V2
## 1: A 1
## 2: A 100
## 3: B 2
## 4: B 100
## It does not work
df %>%
group_by(V1) %>%
filter(V2 == min(V2))
## Source: local data table [4 x 2]
## Groups: V1
##
## V1 V2
## 1 A 1
## 2 A 100
## 3 B 2
## 4 B 100
???
It works.
df = data.frame(V1 = c("A", "A", "B", "B"), V2 = c(1, 100, 2, 100))
df %>%
group_by(V1) %>%
filter(V2 == min(V2))
## Source: local data frame [2 x 2]
## Groups: V1
##
## V1 V2
## 1 A 1
## 2 B 2
df %>% as.data.table %>%
group_by(V1) %>%
filter(V2 == min(V2))
## Error: i has not evaluated to logical, integer or double
By using iris data...
iris %>% as.data.table %>%
group_by(Species) %>%
filter(Sepal.Length == min(Sepal.Length))
## Source: local data table [3 x 5]
## Groups: Species
##
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 4.3 3.0 1.1 0.1 setosa
## 2 4.9 2.4 3.3 1.0 versicolor
## 3 4.9 2.5 4.5 1.7 virginica
It works. why??
sessionInfo()
## R version 3.1.0 (2014-04-10)
## Platform: x86_64-apple-darwin10.8.0 (64-bit)
##
## locale:
## [1] ja_JP.UTF-8/ja_JP.UTF-8/ja_JP.UTF-8/C/ja_JP.UTF-8/ja_JP.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] dplyr_0.2 data.table_1.9.2 knitr_1.6
##
## loaded via a namespace (and not attached):
## [1] assertthat_0.1 evaluate_0.5.5 formatR_1.0 magrittr_1.0.1
## [5] parallel_3.1.0 plyr_1.8.1 Rcpp_0.11.2 reshape2_1.4
## [9] stringr_0.6.2 tools_3.1.0