R: How to output a recursive or iterative function/map output into a vector?
up vote
1
down vote
favorite
I'd like to feed the output of a user-defined function back to its input (a recursive map), run this iteration N times, and save the output of every iteration in a vector. This is simple to do with "for" loop
my_fun <- function(x) {x/3 +1} # a user-defined function (trivial example)
my_l <- c()
x <- 0 # initial condition
for(i in 1:10) {
x <- my_fun(x)
my_l[i] <- x
}
print(my_l)
>[1] 1.000000 1.333333 1.444444 1.481481 1.493827 1.497942 1.499314 1.499771 1.499924 1.499975
The above works but seems crude. Is there a shorter way to do it? Perhaps with tidyverse/purrr?
r loops recursion purrr
add a comment |
up vote
1
down vote
favorite
I'd like to feed the output of a user-defined function back to its input (a recursive map), run this iteration N times, and save the output of every iteration in a vector. This is simple to do with "for" loop
my_fun <- function(x) {x/3 +1} # a user-defined function (trivial example)
my_l <- c()
x <- 0 # initial condition
for(i in 1:10) {
x <- my_fun(x)
my_l[i] <- x
}
print(my_l)
>[1] 1.000000 1.333333 1.444444 1.481481 1.493827 1.497942 1.499314 1.499771 1.499924 1.499975
The above works but seems crude. Is there a shorter way to do it? Perhaps with tidyverse/purrr?
r loops recursion purrr
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'd like to feed the output of a user-defined function back to its input (a recursive map), run this iteration N times, and save the output of every iteration in a vector. This is simple to do with "for" loop
my_fun <- function(x) {x/3 +1} # a user-defined function (trivial example)
my_l <- c()
x <- 0 # initial condition
for(i in 1:10) {
x <- my_fun(x)
my_l[i] <- x
}
print(my_l)
>[1] 1.000000 1.333333 1.444444 1.481481 1.493827 1.497942 1.499314 1.499771 1.499924 1.499975
The above works but seems crude. Is there a shorter way to do it? Perhaps with tidyverse/purrr?
r loops recursion purrr
I'd like to feed the output of a user-defined function back to its input (a recursive map), run this iteration N times, and save the output of every iteration in a vector. This is simple to do with "for" loop
my_fun <- function(x) {x/3 +1} # a user-defined function (trivial example)
my_l <- c()
x <- 0 # initial condition
for(i in 1:10) {
x <- my_fun(x)
my_l[i] <- x
}
print(my_l)
>[1] 1.000000 1.333333 1.444444 1.481481 1.493827 1.497942 1.499314 1.499771 1.499924 1.499975
The above works but seems crude. Is there a shorter way to do it? Perhaps with tidyverse/purrr?
r loops recursion purrr
r loops recursion purrr
asked Nov 5 at 17:00
Irakli
314210
314210
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
We can use accumulate
library(tidyverse)
accumulate(1:10, ~ my_fun(.x), .init = 1)
#[1] 1.000000 1.333333 1.444444 1.481481 1.493827 1.497942 1.499314 1.499771 1.499924 1.499975 1.499992
Or with Reduce
from base R
Reduce(function(x, y) my_fun(x), 1:10, init = 1, accumulate = TRUE)
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
We can use accumulate
library(tidyverse)
accumulate(1:10, ~ my_fun(.x), .init = 1)
#[1] 1.000000 1.333333 1.444444 1.481481 1.493827 1.497942 1.499314 1.499771 1.499924 1.499975 1.499992
Or with Reduce
from base R
Reduce(function(x, y) my_fun(x), 1:10, init = 1, accumulate = TRUE)
add a comment |
up vote
4
down vote
accepted
We can use accumulate
library(tidyverse)
accumulate(1:10, ~ my_fun(.x), .init = 1)
#[1] 1.000000 1.333333 1.444444 1.481481 1.493827 1.497942 1.499314 1.499771 1.499924 1.499975 1.499992
Or with Reduce
from base R
Reduce(function(x, y) my_fun(x), 1:10, init = 1, accumulate = TRUE)
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
We can use accumulate
library(tidyverse)
accumulate(1:10, ~ my_fun(.x), .init = 1)
#[1] 1.000000 1.333333 1.444444 1.481481 1.493827 1.497942 1.499314 1.499771 1.499924 1.499975 1.499992
Or with Reduce
from base R
Reduce(function(x, y) my_fun(x), 1:10, init = 1, accumulate = TRUE)
We can use accumulate
library(tidyverse)
accumulate(1:10, ~ my_fun(.x), .init = 1)
#[1] 1.000000 1.333333 1.444444 1.481481 1.493827 1.497942 1.499314 1.499771 1.499924 1.499975 1.499992
Or with Reduce
from base R
Reduce(function(x, y) my_fun(x), 1:10, init = 1, accumulate = TRUE)
answered Nov 5 at 17:02
akrun
387k13174250
387k13174250
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53158901%2fr-how-to-output-a-recursive-or-iterative-function-map-output-into-a-vector%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password