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

Go To StackoverFlow.com

0

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.

2012-04-04 23:39
by user182814
R is a vectorised language, so if you show us your h we can help you to take advantage of that, such that typing h(1:10) will return c(h(1), h(2), ..., h(10)) - mathematical.coffee 2012-04-04 23:40
Question as posed is vague and open to (mis)interpretation. Please provide your function so we can help you better - Carl Witthoft 2012-04-05 00:21


1

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?

2012-04-04 23:44
by Justin
Ads