[R] loop in list
R. Michael Weylandt <michael.weylandt@gmail.com>
michael.weylandt at gmail.com
Sun Jul 1 02:24:16 CEST 2012
I might think replicate() is slightly more idiomatic, but I'm not in a position to check if simplify=FALSE will keep a list.
Best,
Michael
On Jun 30, 2012, at 7:13 PM, Rui Barradas <ruipbarradas at sapo.pt> wrote:
> Hello,
>
> You can avoid the loop using lapply.
>
> f <- function(x) sample(100, 10)
> samp.list <- lapply(1:20, f)
>
> will choose 20 samples of 10 integers up to 100 and put them in a list. All you need is to write a function f(). f() must have an argument, even if it doesn't use it. If you need other arguments to be processed by f(), define it and call it as, for instance using the example above,
>
> f <- function(x, ...) sample(100, 10, ...)
> lapply(1:20, f) # the same
> lapply(1:20, f, replace=TRUE) # a second argument
>
> See ?lapply
>
> Hope this helps,
>
> Rui Barradas
>
> Em 30-06-2012 20:34, solafah bh escreveu:
>> Hello
>> I have a loop to sample 20 samples and I want to put them in one list, how I can make this??
>>
>> Regards
>> Sulafah
>> [[alternative HTML version deleted]]
>>
>>
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list