简体   繁体   中英

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.

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

PS This isn't homework is it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM