The m61r package provides a grammar for data manipulation prioritising speed and memory efficiency.
The m61r object allows for seamless chaining of operations.
p <- m61r(mtcars)
p$filter(~mpg > 20)
p$select(~c(mpg, cyl, hp))
p$mutate(hp_norm = ~hp / max(hp))
p$head()## mpg cyl hp hp_norm
## 1 21.0 6 110 0.9734513
## 2 21.0 6 110 0.9734513
## 3 22.8 4 93 0.8230088
## 4 21.4 6 110 0.9734513
## 5 24.4 4 62 0.5486726
## 6 22.8 4 95 0.8407080
You can use the .SD() method within a formula to access the current data subset for advanced transformations.
p <- m61r(iris)
p$mutate(scaled = ~lapply(.SD()[, vapply(.SD(), is.numeric, logical(1))],
function(x) x * 100))
p$head(3)## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 510 350 140 20 setosa
## 2 490 300 140 20 setosa
## 3 470 320 130 20 setosa
select_colsThe .select_cols helper allows for clean, functional column selection based on predicates.
tmp <- m61r(mtcars)
tmp$mutate(all_num = ~lapply(.SD()[, .select_cols(.SD(), is.numeric)],
function(x) x * 10))
tmp$head(3)## mpg cyl disp hp drat wt qsec vs am gear carb
## 1 210 60 1600 1100 39.0 26.20 164.6 0 10 40 40
## 2 210 60 1600 1100 39.0 28.75 170.2 0 10 40 40
## 3 228 40 1080 930 38.5 23.20 186.1 10 10 40 10
Easily convert multiple columns at once using standard R functions inside the m61r environment.
p <- m61r(mtcars)
# Convert all columns to numeric using lapply for Base R purity
p$mutate(~lapply(.SD(), as.numeric))GrepUse grep to apply transformations to columns matching specific naming patterns.
p <- m61r(mtcars)
# Normalize only columns that contain the word "mpg" or "hp"
p$mutate(~lapply(.SD()[, grep("mpg|hp", names(.SD()))], function(x) x / max(x)))
p$head()## mpg cyl disp hp drat wt qsec vs am gear carb
## 1 0.6194690 6 160 0.3283582 3.90 2.620 16.46 0 1 4 4
## 2 0.6194690 6 160 0.3283582 3.90 2.875 17.02 0 1 4 4
## 3 0.6725664 4 108 0.2776119 3.85 2.320 18.61 1 1 4 1
## 4 0.6312684 6 258 0.3283582 3.08 3.215 19.44 1 0 3 1
## 5 0.5516224 8 360 0.5223881 3.15 3.440 17.02 0 0 3 2
## 6 0.5339233 6 225 0.3134328 2.76 3.460 20.22 1 0 3 1
The case_when function provides a vectorised way to handle complex conditional assignments.
p <- m61r(mtcars)
p$mutate(category = ~case_when(
mpg > 25, "Eco",
mpg > 18, "Standard",
"High-Perf"
))
head(p[, c("mpg", "category")], 5)## mpg category
## 1 21.0 Standard
## 2 21.0 Standard
## 3 22.8 Standard
## 4 21.4 Standard
## 5 18.7 Standard
The across() function provides a familiar syntax for applying functions across a selection of columns during summarisation.
p <- m61r(mtcars)
p$summarise(
avg = ~across(c("mpg", "disp", "hp"), mean)
)By staying true to Base R, m61r ensures that your code remains portable, fast, and light.