-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Required data.table arguments for the filtering verbs #17
Comments
It would be possible to add parameters to |
Noted. Altering |
Related to above, regarding
This may be redundant if intent is to use # install latest version
# devtools::install_github('asardaes/table.express')
library(dplyr)
library(table.express)
library(data.table)
## Create a data table
DT <- data.table(V1 = rep(c(1L, 2L), 5)[-10],
V2 = 1:9,
V3 = c(0.5, 1.0, 1.5),
V4 = rep(LETTERS[1:3], 3))
setkey(DT, V4)
# scenario 1
DT[c("A", "D"), on = "V4", nomatch = NA]
#> V1 V2 V3 V4
#> 1: 1 1 0.5 A
#> 2: 2 4 0.5 A
#> 3: 1 7 0.5 A
#> 4: NA NA NA D
DT %>%
start_expr() %>%
filter_on(V4 = c("A", "D"), nomatch = NA) %>%
end_expr() %>% {
invisible(print(.))
}
#> V1 V2 V3 V4
#> 1: 1 1 0.5 A
#> 2: 2 4 0.5 A
#> 3: 1 7 0.5 A
#> 4: NA NA NA D
DT %>%
start_expr() %>%
filter_sd(.SDcols = 'V4', c("A", "D")) %>%
end_expr() %>% {
invisible(print(.))
}
#> V1 V2 V3 V4
#> 1: 1 1 0.5 A
#> 2: 2 4 0.5 A
#> 3: 1 7 0.5 A
#> 4: NA NA NA D
# scenario 2
DT[c("A", "D"), on = "V4", nomatch = 0]
#> V1 V2 V3 V4
#> 1: 1 1 0.5 A
#> 2: 2 4 0.5 A
#> 3: 1 7 0.5 A
DT %>%
start_expr() %>%
filter_on(V4 = c("A", "D"), nomatch = 0) %>%
end_expr() %>% {
invisible(print(.))
}
#> V1 V2 V3 V4
#> 1: 1 1 0.5 A
#> 2: 2 4 0.5 A
#> 3: 1 7 0.5 A Created on 2019-07-06 by the reprex package (v0.2.1) |
Well yes and no. Both You could still write something like this if you really wanted to use one of those verbs: DT %>%
start_expr %>%
filter(c("A", "D")) %>%
frame_append(nomatch = NULL) %>%
end_expr As a side note: try to stay away from |
Noted; thanks for reference. Looks like I agree making Another use case current
|
That could work. With the new |
Aye 👍 Apologies for keep expanding the issues; another one to consider Closest solution: create a row index first # with dual indices
# returns the matching rows indices of original DT
DT[.(c("B", "C"), 1), which = TRUE]
#> [1] 4 7 8
DT %>%
start_expr() %>%
mutate(n = .I) %>%
chain %>%
filter(.(c("B", "C"), 1)) %>%
end_expr() %>% {
invisible(print(.))
}
#> V1 V2 V3 V4 n
#> 1: 1 5 1.0 B 4
#> 2: 1 3 1.5 C 7
#> 3: 1 9 1.5 C 8 |
Oh, that one did slip my mind. Yeah, I'll add that. |
👏 # install latest version
# devtools::install_github('asardaes/table.express')
suppressMessages(library(dplyr))
suppressMessages(library(table.express))
suppressMessages(library(data.table))
#> Warning: package 'data.table' was built under R version 3.5.3
## Create a data table
DT <- data.table(V1 = rep(c(1L, 2L), 5)[-10],
V2 = 1:9,
V3 = c(0.5, 1.0, 1.5),
V4 = rep(LETTERS[1:3], 3))
setkey(DT, V4, V1)
# multi-key
DT[.(c("B", "C"), 1)]
#> V1 V2 V3 V4
#> 1: 1 5 1.0 B
#> 2: 1 3 1.5 C
#> 3: 1 9 1.5 C
# using filter()
DT %>%
start_expr() %>%
filter(.(c("B", "C"), 1)) %>%
end_expr() %>% {
invisible(print(.))
}
#> V1 V2 V3 V4
#> 1: 1 5 1.0 B
#> 2: 1 3 1.5 C
#> 3: 1 9 1.5 C
# using filter_on()
DT %>%
start_expr() %>%
filter_on(c("B", "C"), 1) %>%
end_expr() %>% {
invisible(print(.))
}
#> V1 V2 V3 V4
#> 1: 1 5 1.0 B
#> 2: 1 3 1.5 C
#> 3: 1 9 1.5 C
# using which = TRUE only returns the matching rows indices
DT[.(c("B", "C"), 1), on = .(V4, V1), which = TRUE]
#> [1] 4 7 8
# which argument in data.table filter
DT %>%
start_expr() %>%
filter_on(c("B", "C"), 1, which = TRUE) %>%
end_expr() %>% {
invisible(print(.))
}
#> [1] 4 7 8 Created on 2019-07-07 by the reprex package (v0.2.1) |
Reprex below.
Goal: keep
filter_on()
consistent withdata.table
filter-on, with and without index set onDT
- refer to ex1 and ex2 below.Proposal: add
mult
argument tofilter_sd()
or alterfilter_on()
to honour index set inDT
; leavefilter
as-is since it's imported fromdplyr
.Created on 2019-07-06 by the reprex package (v0.2.1)
The text was updated successfully, but these errors were encountered: