owned this note
owned this note
Published
Linked with GitHub
# Some of the updates and additions in R-4.1.0
## New Syntax
### Native Pipes
```r
library(magrittr)
iris %>%
filter(Species %in% c("setosa", "versicolor")) %>%
head()
iris |>
filter(Species %in% c("setosa", "versicolor")) |>
head()
```
```r
f <- function(x) x^2
g <- function(y) y*2
f(g(10))
10 %>% g() %>% f()
10 |> g() |> f()
```
#### Stack traces are easier to read?
## Try with something that throws an error
```r
"test" %>% g() %>% f()
traceback()
"test" |> g() |> f()
traceback()
```
```r
iris %>%
filter(Species %in% c("setosa", "versicolor")) %>%
group_by(Sepal.Width) %>%
summarise(`Mean width` = mean(Sepal.Girth)) %>%
mutate(`Mean width` = round(`Mean width`, 1))
rlang::last_error()
iris |>
filter(Species %in% c("setosa", "versicolor")) |>
group_by(Sepal.Width) |>
summarise(`Mean width` = mean(Sepal.Girth)) |>
mutate(`Mean width` = round(`Mean width`, 1))
rlang::last_error()
```
### Anonymous functions
```r
function(x) x + 1
\(x) x + 1
sapply(1:10, FUN = \(x) x + 1)
```
### Limitations of native pipes
```r
c("vector", "matrix", "array") %>%
grepl(pattern = "ar", x = .)
c("vector", "matrix", "array") |>
grepl(pattern = "ar", x = .)
```
```r
find_at <- function(x) grepl(pattern = "at", x = x)
c("vector", "matrix", "array") |>
find_at()
c("vector", "matrix", "literate") |>
{\(x) grepl(pattern = "at", x = x)}()
```
## Improvements & additions
### Concatenating factors works as I might expect
Try this in R-4.0 vs R-4.1. The former is not what I'd expect!
```r
n <- 500
fkt1 <- factor(sample(letters, n, TRUE))
fkt2 <- factor(sample(LETTERS, n, TRUE))
head(
c(fkt1, fkt2)
)
```
### Improved performance for `grep()` et al on factors
```r
nn <- 1e7
fkt <- factor(sample(letters, nn, TRUE))
system.time(
grepl("[k-p]", fkt)
)
```
```r
system.time(
gsub("[k-p]", "xx", fkt)
)
```
### Added `gregexec` function.
```r
pattern <- "([[:alpha:]]+)([[:digit:]]+)"
s <- "Test: A1 BC23 DEF456"
regexec(pattern, s)
gregexec(pattern, s)
```
### Improved performance of duplicated() on sorted vectors
```r
set.seed(1234)
x <- sample(1e3, 1e7, replace = TRUE)
y <- sort(x)
z <- y
## this stops z from knowing it is sorted
z[1] <- z[1]
system.time(yd <- duplicated(y))
system.time(zd <- duplicated(z))
identical(yd, zd)
```
### Splitting data.frames using "~"
```r
## R-4.0.5
split(mtcars, f = mtcars$gear)
## R-4.1.0
split(mtcars, f = ~ gear)
```
### Locking base environment
```r
## when would you do this?
test <- baseenv()
test$foo <- 1:10
```
## Extras
### The default C++ standard has been changed to C++14
### No more 32-bit Windows after R-4.1
### Translation in to Lithuanian
Tutorial at useR 2021 if this is interesting to you!
https://www.conftool.org/user2021/index.php?page=browseSessions&search=Translating+R