I want to translate the following R
code using tidytable into collapse: Advanced and Fast Data Transformation.
tidytable Code
library(tidytable)
library(collapse)
Out1 <-
wlddev %>%
mutate_rowwise.(New1 = sum(c_across.(PCGDP:GINI), na.rm = TRUE))
Out1 %>%
select.(New1)
# A tidytable: 13,176 x 1
New1
<dbl>
1 32.4
2 33.0
3 33.5
4 34.0
5 34.5
6 34.9
7 35.4
8 35.9
9 36.4
10 36.9
# ... with 13,166 more rows
collapse Code
library(collapse)
Out2 <-
wlddev %>%
ftransform(New1 = fsum(across(PCGDP:GINI), na.rm = TRUE))
Error in `context_peek()`:
! `across()` must only be used inside dplyr verbs.
Run `rlang::last_error()` to see where the error occurred.
Any hint please.
I wonder why you need to come up with something so complex. You have functions like rowSums
in base R, and you have parallel statistical functions in kit
:
library(collapse)
library(magrittr)
library(kit, include.only = "psum")
library(microbenchmark)
microbenchmark(
A = wlddev %>%
ftransform(New1 = rowSums(qM(slt(., PCGDP:GINI)), na.rm = TRUE)),
B = wlddev %>%
ftransform(New1 = psum(slt(., PCGDP:GINI), na.rm = TRUE)),
C = wlddev %>%
ftransform(New1 = psum(PCGDP, LIFEEX, GINI, na.rm = TRUE))
)
#> Unit: microseconds
#> expr min lq mean median uq max neval
#> A 68.88 97.8875 194.24037 102.2335 113.8775 4646.366 100
#> B 25.83 30.1350 35.43548 34.9115 38.6630 56.416 100
#> C 22.55 25.8095 29.99396 30.5860 32.9025 53.792 100
Created on 2022-02-05 by the reprex package (v2.0.1)