Post

Shorthand notation to create functions in r

Shorthand notation to create functions in r
1
numbers <- c(1, 2, 3, 4, 5)
1
vapply(numbers, function(x) x * 2, numeric(1))
1
[1]  2  4  6  8 10

Create an anonymous function by shorthand annotation

1
vapply(numbers, \(x) x * 2, numeric(1))
1
[1]  2  4  6  8 10

Create a function by shorthand annotation

1
2
times_two <- \(x) x * 2
vapply(numbers, times_two, numeric(1))
1
[1]  2  4  6  8 10

Trending Tags