user182814
user182814

Reputation: 9

How can I construct this list from this function in R?

I have a function h(n) that returns values for each integer 1 =< n =< k.

How can I construct a list of the form (h(1), h(2), h(3), ...) where k is large so doing this manually will take some time.

Upvotes: 0

Views: 44

Answers (1)

Justin
Justin

Reputation: 43255

Without your function, I don't know for sure, but lapply(1:k, h) should take every value between 1 and k and send it to your function and return them in a list.

> h <- function(n) return(1:n)
> lapply(1:5, h)
[[1]]
[1] 1

[[2]]
[1] 1 2

[[3]]
[1] 1 2 3

[[4]]
[1] 1 2 3 4

[[5]]
[1] 1 2 3 4 5

P.S. This isn't homework is it?

Upvotes: 1

Related Questions